1 1
import * as fs from 'fs'
2 1
import * as path from 'path'
3
import * as Rsg from 'react-styleguidist'
4

5
/**
6
 * Extract example from file
7
 */
8 1
const importRE = /^\[import\]\(([./\w]+)\)/
9

10 1
export default function importCodeExampleFile(
11
	example: Pick<Rsg.CodeExample, 'content' | 'lang' | 'settings'>,
12
	mdPath: string,
13 1
	wp: any
14
): Pick<Rsg.CodeExample, 'content' | 'lang' | 'settings'> {
15 1
	const lang = importRE.exec(example.lang || '')
16 1
	if (lang) {
17 0
		const filePath = lang[1]
18 0
		const absoluteFilePath = path.resolve(path.dirname(mdPath), filePath)
19 0
		wp.addDependency(absoluteFilePath)
20 0
		example.content = fs.readFileSync(absoluteFilePath, 'utf8')
21 0
		example.lang = path.extname(filePath).slice(1)
22

23 0
		const folderPath = path.dirname(filePath)
24 1
		if (folderPath !== '.') {
25 0
			example.settings = { ...example.settings, importpath: folderPath }
26
		}
27
	}
28 1
	return example
29
}

Read our documentation on viewing source code .

Loading