-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: automatics better type declarations.
- Loading branch information
Showing
38 changed files
with
161 additions
and
574 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { fileURLToPath } from 'node:url' | ||
import fs from 'fs-extra' | ||
import { resolve } from 'pathe' | ||
import type { Plugin } from 'vite' | ||
|
||
import { getTypeDeclaration } from '../utils' | ||
|
||
export function markdownTransform(): Plugin { | ||
const DIR_CORE = resolve(fileURLToPath(import.meta.url), '../../../core') | ||
const DIR_TYPES = resolve(fileURLToPath(import.meta.url), '../../../tsc-types/src/core') | ||
const hasTypes = fs.existsSync(DIR_TYPES) | ||
|
||
if (!hasTypes) | ||
console.warn('No types dist found, run `pnpm build:types` first.') | ||
|
||
return { | ||
name: 'docs-md-transform', | ||
enforce: 'pre', | ||
async transform(code, id) { | ||
if (!id.match(/\.md\b/)) | ||
return | ||
|
||
const [pkg, name, i] = id.split('/').slice(-3) | ||
const lang = langMap(i.split('-')[1].replace(/\.md$/, '') ?? 'en') | ||
|
||
const types = await getTypeDeclaration(pkg, name) | ||
|
||
const URL = 'https://github.com/s3xysteak/cesium-use/blob/main/src/core' | ||
|
||
const existDemo = fs.existsSync(`${DIR_CORE}/${pkg}/${name}/demo.vue`) | ||
|
||
const existsIndex = fs.existsSync(`${DIR_CORE}/${pkg}/${name}/index.ts`) | ||
|
||
return `${code} | ||
${types | ||
? ` | ||
## ${lang?.type} | ||
::: details | ||
\`\`\`ts | ||
${types} | ||
\`\`\` | ||
::: | ||
` | ||
: ''} | ||
## ${lang?.source} | ||
[source](${`${URL}/${pkg}/${name}/index.${existsIndex ? 'ts' : 'vue'}`})${existDemo ? ` • [demo](${`${URL}/core/${pkg}/${name}/demo.vue`})` : ''}` | ||
}, | ||
} | ||
} | ||
|
||
function langMap(lang: string) { | ||
return { | ||
en: { | ||
type: 'Type Declarations', | ||
source: 'Source', | ||
}, | ||
zh: { | ||
type: '类型声明', | ||
source: '源码', | ||
}, | ||
}[lang] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { fileURLToPath } from 'node:url' | ||
import fs from 'fs-extra' | ||
import { join, resolve } from 'pathe' | ||
|
||
const DIR_TYPES = resolve(fileURLToPath(import.meta.url), '../../../tsc-types/src/core') | ||
|
||
export async function getTypeDeclaration(pkg: string, name: string): Promise<string | undefined> { | ||
const typingFilepath = join(DIR_TYPES, `${pkg}/${name}/index.d.ts`) | ||
|
||
if (!fs.existsSync(typingFilepath)) | ||
return | ||
|
||
let types = await fs.readFile(typingFilepath, 'utf-8') | ||
|
||
if (!types) | ||
return | ||
|
||
// clean up types | ||
types = types | ||
.replace('/// <reference types="cesium/Source/Cesium.js" />', '') | ||
.replace(/import\(.*?\)\./g, '') | ||
.replace(/import[\s\S]+?from ?["'][\s\S]+?["']/g, '') | ||
.replace(/export {}/g, '') | ||
|
||
const prettier = await import('prettier') | ||
return (await prettier | ||
.format( | ||
types, | ||
{ | ||
semi: false, | ||
parser: 'typescript', | ||
}, | ||
)) | ||
.trim() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.