Navigation | Overlay |
---|---|
t Navigate files | h Toggle hits |
y Change url to tip of branch | m Toggle misses |
b / v Jump to prev/next hit line | p Toggle partial |
z / x Jump to prev/next missed or partial line | 1..9 Toggle flags |
shift + o Open current page in GitHub | a Toggle all on |
/ or ? Show keyboard shortcuts dialog | c Toggle context lines or commits |
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 |
const lang = importRE.exec(example.lang || '') |
|
16 |
if (lang) { |
|
17 |
const filePath = lang[1] |
|
18 |
const absoluteFilePath = path.resolve(path.dirname(mdPath), filePath) |
|
19 |
wp.addDependency(absoluteFilePath) |
|
20 |
example.content = fs.readFileSync(absoluteFilePath, 'utf8') |
|
21 |
example.lang = path.extname(filePath).slice(1) |
|
22 |
|
|
23 |
const folderPath = path.dirname(filePath) |
|
24 |
if (folderPath !== '.') { |
|
25 |
example.settings = { ...example.settings, importpath: folderPath } |
|
26 |
}
|
|
27 |
}
|
|
28 | 1 |
return example |
29 |
}
|
Read our documentation on viewing source code .