-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
markdownTransform.ts
157 lines (126 loc) · 4.23 KB
/
markdownTransform.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { join, resolve } from 'path'
import type { Plugin } from 'vite'
import fs from 'fs-extra'
import { packages } from '../../../meta/packages'
import { functionNames, getFunction } from '../../../packages/metadata/metadata'
import { getTypeDefinition, replacer } from '../../../scripts/utils'
export function MarkdownTransform(): Plugin {
const DIR_TYPES = resolve(__dirname, '../../../types/packages')
const hasTypes = fs.existsSync(DIR_TYPES)
if (!hasTypes)
console.warn('No types dist found, run `npm run build:types` first.')
return {
name: 'vueuse-md-transform',
enforce: 'pre',
async transform(code, id) {
if (!id.match(/\.md\b/))
return null
// linkify function names
code = code.replace(
new RegExp(`\`({${functionNames.join('|')}})\`(.)`, 'g'),
(_, name, ending) => {
if (ending === ']') // already a link
return _
const fn = getFunction(name)!
return `[\`${fn.name}\`](${fn.docs})`
},
)
// convert links to relative
code = code.replace(/https?:\/\/vueuse\.org\//g, '/')
const [pkg, _name, i] = id.split('/').slice(-3)
const name = functionNames.find(n => n.toLowerCase() === _name.toLowerCase()) || _name
if (functionNames.includes(name) && i === 'index.md') {
const frontmatterEnds = code.indexOf('---\n\n') + 4
const firstSubheader = code.search(/\n## \w/)
const sliceIndex = firstSubheader < 0 ? frontmatterEnds : firstSubheader
const { footer, header } = await getFunctionMarkdown(pkg, name)
if (hasTypes)
code = replacer(code, footer, 'FOOTER', 'tail')
if (header)
code = code.slice(0, sliceIndex) + header + code.slice(sliceIndex)
code = code
.replace(/(# \w+?)\n/, `$1\n\n<FunctionInfo fn="${name}"/>\n`)
.replace(/## (Components?(?:\sUsage)?)/i, '## $1\n<LearnMoreComponents />\n\n')
.replace(/## (Directives?(?:\sUsage)?)/i, '## $1\n<LearnMoreDirectives />\n\n')
}
return code
},
}
}
const DIR_SRC = resolve(__dirname, '../..')
const GITHUB_BLOB_URL = 'https://github.com/vueuse/vueuse/blob/main/packages'
export async function getFunctionMarkdown(pkg: string, name: string) {
const URL = `${GITHUB_BLOB_URL}/${pkg}/${name}`
const dirname = join(DIR_SRC, pkg, name)
const demoPath = ['demo.vue', 'demo.client.vue'].find(i => fs.existsSync(join(dirname, i)))
const types = await getTypeDefinition(pkg, name)
let typingSection = ''
if (types) {
const code = `\`\`\`typescript\n${types.trim()}\n\`\`\``
typingSection = types.length > 1000
? `
## Type Declarations
<details>
<summary op50 italic cursor-pointer select-none>Show Type Declarations</summary>
${code}
</details>
`
: `\n## Type Declarations\n\n${code}`
}
const links = ([
['Source', `${URL}/index.ts`],
demoPath ? ['Demo', `${URL}/${demoPath}`] : undefined,
['Docs', `${URL}/index.md`],
])
.filter(i => i)
.map(i => `[${i![0]}](${i![1]})`).join(' • ')
const sourceSection = `## Source\n\n${links}\n`
const ContributorsSection = `
## Contributors
<Contributors fn="${name}" />
`
const changelogSection = `
## Changelog
<Changelog fn="${name}" />
`
const demoSection = demoPath
? demoPath.endsWith('.client.vue')
? `
<script setup>
import { defineAsyncComponent } from 'vue'
const Demo = defineAsyncComponent(() => import('./${demoPath}'))
</script>
## Demo
<DemoContainer>
<p class="demo-source-link"><a href="${URL}/${demoPath}" target="_blank">source</a></p>
<ClientOnly>
<Suspense>
<Demo/>
<template #fallback>
Loading demo...
</template>
</Suspense>
</ClientOnly>
</DemoContainer>
`
: `
<script setup>
import Demo from \'./${demoPath}\'
</script>
## Demo
<DemoContainer>
<p class="demo-source-link"><a href="${URL}/${demoPath}" target="_blank">source</a></p>
<Demo/>
</DemoContainer>
`
: ''
const packageNote = packages.find(p => p.name === pkg)!.addon
? `Available in the <a href="/${pkg}/README">@vueuse/${pkg}</a> add-on.\n`
: ''
const footer = `${typingSection}\n\n${sourceSection}\n${ContributorsSection}\n${changelogSection}\n`
const header = demoSection + packageNote
return {
footer,
header,
}
}