Skip to content

Commit 45880f2

Browse files
docs: include example as snippets in the exported markdown (#21947)
Co-authored-by: Andrew Henry <[email protected]> Resolves #21711
1 parent 25af169 commit 45880f2

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

packages/docs/src/composables/markdown.ts

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export function useMarkdown () {
2020
return
2121
}
2222

23-
let markdownContent = `# ${frontmatter.value?.meta.title || 'Page Title'}\\n\\n`
24-
markdownContent += `Source: ${window.location.origin}${route.path}\\n\\n`
23+
let markdownContent = `# ${frontmatter.value?.meta.title || 'Page Title'}\n\n`
24+
markdownContent += `Source: ${window.location.origin}${route.path}\n\n`
2525

2626
try {
2727
const path = `packages/docs/src/pages${route.path.replace(/\/$/, '')}.md`
@@ -35,7 +35,54 @@ export function useMarkdown () {
3535

3636
if (response.data && 'content' in response.data) {
3737
const rawMd = atob(response.data.content)
38-
const contentWithoutFrontmatter = rawMd.replace(/---[\\s\\S]*?---/, '').trim()
38+
let processedMd = rawMd
39+
40+
// Remove <ApiInline /> and <ExamplesUsage /> tags completely
41+
processedMd = processedMd
42+
.replace(/<ApiInline[\s\S]*?>([\s\S]*?\/>\n\n)?/g, '')
43+
.replace(/<ExamplesUsage[\s\S]*?>([\s\S]*?\/>\n\n)?/g, '')
44+
45+
// Replace every <ExamplesExample ...> tag with the raw Vue source of its file
46+
if (processedMd.includes('<ExamplesExample')) {
47+
const tagRegex = /<ExamplesExample\b([^>]*?)>(?:[\s\S]*?<\/ExamplesExample>)|<ExamplesExample\b([^>]*?)\/>/g
48+
const matches = [...processedMd.matchAll(tagRegex)]
49+
50+
for (const m of matches) {
51+
const attrs = m[1] ?? m[2] ?? ''
52+
const fileMatch = attrs.match(/\bfile\s*=\s*"([^"]+)"/)
53+
if (!fileMatch) {
54+
// Remove tag if no file attribute
55+
processedMd = processedMd.replace(m[0], '')
56+
continue
57+
}
58+
59+
const filePath = fileMatch[1].replace(/\\+/g, '/').replace(/\/$/, '')
60+
const pathExamples = `packages/docs/src/examples/${filePath}.vue`
61+
62+
try {
63+
const responseExamples = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
64+
owner: 'vuetifyjs',
65+
repo: 'vuetify',
66+
path: pathExamples,
67+
ref: branch,
68+
})
69+
70+
if (responseExamples.data && 'content' in responseExamples.data) {
71+
const rawVue = atob(responseExamples.data.content)
72+
// Replace whole tag with fenced code block of the example source
73+
processedMd = processedMd.replace(m[0], `\n\`\`\`vue\n${rawVue}\n\`\`\`\n`)
74+
} else {
75+
// If fetch fails, strip the tag to avoid leaving placeholders
76+
processedMd = processedMd.replace(m[0], '')
77+
}
78+
} catch (e) {
79+
console.error('Error fetching example file', filePath, e)
80+
processedMd = processedMd.replace(m[0], '')
81+
}
82+
}
83+
}
84+
85+
const contentWithoutFrontmatter = processedMd.replace(/---[\s\S]*?---/, '').trim()
3986
markdownContent += contentWithoutFrontmatter
4087
} else {
4188
markdownContent += 'Could not fetch page content.'

0 commit comments

Comments
 (0)