(_, options)
| 9 | const pluginName = 'component-docs-plugin'; |
| 10 | |
| 11 | async function componentsPlugin(_, options) { |
| 12 | const { docsRootDir, libsRootDir, pages } = options; |
| 13 | |
| 14 | function clean() { |
| 15 | fs.rmSync(docsRootDir, { recursive: true, force: true }); |
| 16 | } |
| 17 | |
| 18 | function createCategory(label, dir = '.') { |
| 19 | const categoryJSON = JSON.stringify({ label }, undefined, 2); |
| 20 | const docsCategoryDir = path.join(docsRootDir, dir); |
| 21 | if (!fs.existsSync(docsCategoryDir)) { |
| 22 | fs.mkdirSync(docsCategoryDir); |
| 23 | } |
| 24 | fs.writeFileSync( |
| 25 | path.join(docsCategoryDir, `_category_.json`), |
| 26 | categoryJSON |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | function createPageForComponent(targetArray, source) { |
| 31 | const [item, subitem] = targetArray; |
| 32 | const target = subitem ? `${item}/${subitem}` : item; |
| 33 | const targetPath = target + '.mdx'; |
| 34 | const sourcePath = source + '.tsx'; |
| 35 | |
| 36 | // Parse component using component-docs. |
| 37 | const doc = parseComponentDocs(path.join(libsRootDir, sourcePath), { |
| 38 | root: libsRootDir, |
| 39 | }); |
| 40 | |
| 41 | // Create directory for the output mdx file. |
| 42 | fs.mkdirSync( |
| 43 | path.join(docsRootDir, targetPath).split('/').slice(0, -1).join('/'), |
| 44 | { recursive: true } |
| 45 | ); |
| 46 | |
| 47 | // Generate and write mdx file. |
| 48 | fs.writeFileSync( |
| 49 | path.join(docsRootDir, targetPath), |
| 50 | generatePageMDX(doc, source) |
| 51 | ); |
| 52 | |
| 53 | return doc; |
| 54 | } |
| 55 | |
| 56 | return { |
| 57 | name: pluginName, |
| 58 | async loadContent() { |
| 59 | // Clean up docs directory. |
| 60 | clean(); |
| 61 | // Create root components category. |
| 62 | createCategory('Components', '.'); |
| 63 | |
| 64 | const docs = {}; |
| 65 | |
| 66 | for (const item in pages) { |
| 67 | if (typeof pages[item] === 'string') { |
| 68 | const doc = createPageForComponent([item], pages[item]); |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…