vue-styleguidist / vue-styleguidist
1 1
import { dirname, join, relative } from 'path'
2 1
import { readFile as rf } from 'fs'
3 1
import { promisify } from 'util'
4
import { ComponentDoc, Tag, ParamTag } from 'vue-docgen-api'
5 1
import { findFileCaseInsensitive } from './utils'
6

7 1
const readFile = promisify(rf)
8

9 1
export function getExamplesFilePaths(
10
	tags: { [key: string]: (Tag | ParamTag)[] },
11 0
	componentDirname: string
12
): string[] {
13 1
	const exampleTags = [...(tags.example || []), ...(tags.examples || [])]
14

15 0
	return (
16 1
		exampleTags.map((exampleTag: ParamTag | Tag) =>
17 0
			isParamTag(exampleTag)
18 1
				? join(componentDirname, exampleTag.description as string)
19 0
				: join(componentDirname, exampleTag.content as string)
20 0
		) || []
21
	)
22
}
23

24 1
export default async function getDocsBlocks(
25
	absolutePath: string,
26
	doc: Pick<ComponentDoc, 'tags' | 'docsBlocks'>,
27
	getDocFileName: (file: string) => string | false,
28
	rootPath: string,
29
	editLinkLabel: string,
30 0
	getRepoEditUrl?: (path: string) => string
31
): Promise<string[]> {
32 1
	const docsBlocks = doc.docsBlocks || []
33

34 0
	const docFilePath = getRepoEditUrl
35 1
		? findFileCaseInsensitive(getDocFileName(absolutePath) || '')
36 0
		: getDocFileName(absolutePath)
37 1
	if (docFilePath) {
38 0
		docsBlocks.push(`${
39 0
			getRepoEditUrl
40 1
				? `
41
<a href="${getRepoEditUrl(
42
						relative(rootPath, docFilePath)
43
				  )}" class="docgen-edit-link">${editLinkLabel}</a>
44
`
45 0
				: ''
46
		}
47 0
${await readFile(docFilePath, 'utf8')}`)
48
	}
49

50
	// load @examples tags into the docsBlocks
51 1
	if (doc.tags?.example || doc.tags?.examples) {
52 0
		const componentDirname = dirname(absolutePath)
53 0
		const examplesFilePaths = getExamplesFilePaths(doc.tags, componentDirname)
54 0
		await Promise.all(
55 0
			examplesFilePaths.map(async examplePath => {
56 1
				const ep = getRepoEditUrl ? findFileCaseInsensitive(examplePath) : examplePath
57 1
				if (ep) {
58 0
					docsBlocks.push(`${
59 0
						getRepoEditUrl
60 1
							? `
61
<a href="${getRepoEditUrl(relative(rootPath, ep))}" class="docgen-edit-link">${editLinkLabel}</a>
62
`
63 0
							: ''
64
					}
65 0
${await readFile(ep, 'utf8')}`)
66
				}
67
			})
68
		)
69
	}
70

71 1
	return docsBlocks
72
}
73

74 1
export function isParamTag(tag: ParamTag | Tag): tag is ParamTag {
75 0
	return !!(tag as ParamTag).description
76
}

Read our documentation on viewing source code .

Loading