microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
npm/generate_katas_content.js
141lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | // @ts-check |
| 5 | |
| 6 | import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; |
| 7 | import { basename, dirname, join } from "node:path"; |
| 8 | import { fileURLToPath } from "node:url"; |
| 9 | import { inspect } from "node:util"; |
| 10 | |
| 11 | import { marked } from "marked"; |
| 12 | |
| 13 | import { katas } from "../katas/content/katas.js"; |
| 14 | |
| 15 | const thisDir = dirname(fileURLToPath(import.meta.url)); |
| 16 | const katasContentDir = join(thisDir, "..", "katas", "content"); |
| 17 | const katasGeneratedContentDir = join(thisDir, "src"); |
| 18 | |
| 19 | function getTitleFromMarkdown(markdown) { |
| 20 | const titleRe = /#+ /; |
| 21 | const lines = markdown.split(/\r?\n/); |
| 22 | if (lines.length === 0) { |
| 23 | throw new Error("Could not parse title, markdown is empty"); |
| 24 | } |
| 25 | |
| 26 | const firstLine = lines[0]; |
| 27 | const match = firstLine.match(titleRe); |
| 28 | if (match === null) { |
| 29 | throw new Error( |
| 30 | `First line does not follow the expected title pattern: ${firstLine}` |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | return firstLine.replace(titleRe, ""); |
| 35 | } |
| 36 | |
| 37 | function buildExampleContent(id, directory) { |
| 38 | const source = readFileSync(join(directory, "example.qs"), "utf8"); |
| 39 | const contentAsMarkdown = readFileSync(join(directory, "content.md"), "utf8"); |
| 40 | const contentAsHtml = marked.parse(contentAsMarkdown); |
| 41 | const title = getTitleFromMarkdown(contentAsMarkdown); |
| 42 | return { |
| 43 | type: "example", |
| 44 | id: id, |
| 45 | title: title, |
| 46 | source: source, |
| 47 | contentAsMarkdown: contentAsMarkdown, |
| 48 | contentAsHtml: contentAsHtml, |
| 49 | }; |
| 50 | } |
| 51 | |
| 52 | function buildExerciseContent(id, directory) { |
| 53 | const placeholderSource = readFileSync( |
| 54 | join(directory, "placeholder.qs"), |
| 55 | "utf8" |
| 56 | ); |
| 57 | const referenceSource = readFileSync(join(directory, "reference.qs"), "utf8"); |
| 58 | const verificationSource = readFileSync(join(directory, "verify.qs"), "utf8"); |
| 59 | const contentAsMarkdown = readFileSync(join(directory, "content.md"), "utf8"); |
| 60 | const contentAsHtml = marked.parse(contentAsMarkdown); |
| 61 | const title = getTitleFromMarkdown(contentAsMarkdown); |
| 62 | return { |
| 63 | type: "exercise", |
| 64 | id: id, |
| 65 | title: title, |
| 66 | placeholderImplementation: placeholderSource, |
| 67 | referenceImplementation: referenceSource, |
| 68 | verificationImplementation: verificationSource, |
| 69 | contentAsMarkdown: contentAsMarkdown, |
| 70 | contentAsHtml: contentAsHtml, |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | function buildReadingContent(id, directory) { |
| 75 | const contentAsMarkdown = readFileSync(join(directory, "content.md"), "utf8"); |
| 76 | const contentAsHtml = marked.parse(contentAsMarkdown); |
| 77 | const title = getTitleFromMarkdown(contentAsMarkdown); |
| 78 | return { |
| 79 | type: "reading", |
| 80 | id: id, |
| 81 | title: title, |
| 82 | contentAsMarkdown: contentAsMarkdown, |
| 83 | contentAsHtml: contentAsHtml, |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | function buildItemContent(item, kataDir) { |
| 88 | const itemDir = join(kataDir, item.directory); |
| 89 | const itemId = `${basename(kataDir)}__${item.directory}`; |
| 90 | if (item.type === "example") { |
| 91 | return buildExampleContent(itemId, itemDir); |
| 92 | } else if (item.type === "exercise") { |
| 93 | return buildExerciseContent(itemId, itemDir); |
| 94 | } else if (item.type === "reading") { |
| 95 | return buildReadingContent(itemId, itemDir); |
| 96 | } |
| 97 | |
| 98 | throw new Error(`Unknown module type ${item.type}`); |
| 99 | } |
| 100 | |
| 101 | function buildKataContent(kata, katasDir) { |
| 102 | const kataDir = join(katasDir, kata.directory); |
| 103 | let itemsContent = []; |
| 104 | for (const item of kata.items) { |
| 105 | const moduleContent = buildItemContent(item, kataDir); |
| 106 | itemsContent.push(moduleContent); |
| 107 | } |
| 108 | |
| 109 | const contentAsMarkdown = readFileSync(join(kataDir, "content.md"), "utf8"); |
| 110 | const contentAsHtml = marked.parse(contentAsMarkdown); |
| 111 | const title = getTitleFromMarkdown(contentAsMarkdown); |
| 112 | return { |
| 113 | id: kata.directory, |
| 114 | title: title, |
| 115 | contentAsMarkdown: contentAsMarkdown, |
| 116 | contentAsHtml: contentAsHtml, |
| 117 | items: itemsContent, |
| 118 | }; |
| 119 | } |
| 120 | |
| 121 | function buildKatasContentJs(katasDir, outDir) { |
| 122 | console.log("Building katas content"); |
| 123 | var katasContent = []; |
| 124 | for (const kata of katas) { |
| 125 | var kataContent = buildKataContent(kata, katasDir); |
| 126 | katasContent.push(kataContent); |
| 127 | } |
| 128 | |
| 129 | if (!existsSync(outDir)) { |
| 130 | mkdirSync(outDir); |
| 131 | } |
| 132 | |
| 133 | const contentJsPath = join(outDir, "katas-content.generated.ts"); |
| 134 | writeFileSync( |
| 135 | contentJsPath, |
| 136 | "export const katas = " + inspect(katasContent, { depth: null }), |
| 137 | "utf-8" |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | buildKatasContentJs(katasContentDir, katasGeneratedContentDir); |
| 142 | |