-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #59
- Loading branch information
Showing
4 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import fs from "node:fs/promises"; | ||
import path from "node:path/posix"; | ||
import markdownIt from "markdown-it"; | ||
import { type Document } from "../document"; | ||
import { ProcessorOptions, type Processor } from "../processor"; | ||
import { haveOutput, normalizePath } from "../utils"; | ||
import { parseInfostring } from "../examples"; | ||
|
||
/** | ||
* Options for `extractExamplesProcessor`. | ||
* | ||
* @public | ||
*/ | ||
export interface ExtractExamplesOptions extends ProcessorOptions { | ||
output: string; | ||
languages?: string[]; | ||
} | ||
|
||
interface Example { | ||
content: string; | ||
language: string; | ||
tags: string[]; | ||
} | ||
|
||
type DocumentWithOutput = Document & { fileInfo: { outputName: string } }; | ||
type DocumentLike = Pick<DocumentWithOutput, "fileInfo" | "body" | "format">; | ||
|
||
const md = markdownIt(); | ||
|
||
md.renderer.rules.fence = (tokens, idx, _options, collected: Example[]) => { | ||
const { content, info } = tokens[idx]; | ||
const { language, tags } = parseInfostring(info); | ||
|
||
if (language === "import") { | ||
return ""; | ||
} | ||
|
||
collected.push({ | ||
content, | ||
language, | ||
tags, | ||
}); | ||
|
||
return ""; | ||
}; | ||
|
||
function findExamples(doc: DocumentLike): Example[] { | ||
if (doc.format !== "markdown") { | ||
return []; | ||
} | ||
const collected: Example[] = []; | ||
md.render(doc.body, collected); | ||
return collected; | ||
} | ||
|
||
function getExtension(lang: string): string { | ||
switch (lang) { | ||
case "typescript": | ||
return "ts"; | ||
case "javascript": | ||
return "js"; | ||
default: | ||
return lang; | ||
} | ||
} | ||
|
||
/** | ||
* Processor to extract and write examples to separate files. | ||
* | ||
* @public | ||
*/ | ||
export function extractExamplesProcessor( | ||
options: ExtractExamplesOptions, | ||
): Processor { | ||
const { | ||
enabled = true, | ||
output, | ||
languages = ["javascript", "typescript", "css", "scss"], | ||
} = options; | ||
|
||
function isRelevant(example: Example): boolean { | ||
return languages.includes(example.language); | ||
} | ||
|
||
return { | ||
stage: "render", | ||
name: "extract-examples-processor", | ||
async handler(context) { | ||
if (!enabled) { | ||
return; | ||
} | ||
|
||
let total = 0; | ||
for (const doc of context.docs.filter(haveOutput)) { | ||
const examples = findExamples(doc).filter(isRelevant); | ||
if (examples.length === 0) { | ||
continue; | ||
} | ||
|
||
/* create a directory matching the filename the examples is contained in */ | ||
const dir = normalizePath(output, doc.fileInfo.fullPath); | ||
await fs.rm(dir, { recursive: true, force: true }); | ||
await fs.mkdir(dir, { recursive: true }); | ||
|
||
/* write each example into the directory */ | ||
let n = 1; | ||
for (const example of examples) { | ||
const extension = getExtension(example.language); | ||
const filename = `example-${String(n++)}.${extension}`; | ||
const filePath = path.join(dir, filename); | ||
await fs.writeFile(filePath, example.content, "utf-8"); | ||
total++; | ||
} | ||
} | ||
|
||
context.log(total, "examples extracted"); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters