-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(reference): generate HTML files with the deno/doc jsr packag…
…e instead of CLI (#1170) Co-authored-by: Josh Collinsworth <joshuajcollinsworth@gmail.com>
- Loading branch information
1 parent
afe9ad6
commit 1c2fae3
Showing
23 changed files
with
866 additions
and
138 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
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,10 @@ | ||
import Prism from "npm:prismjs@1.29.0"; | ||
import "npm:prismjs@1.29.0/components/prism-typescript.js"; | ||
import "npm:prismjs@1.29.0/components/prism-diff.js"; | ||
import "npm:prismjs@1.29.0/components/prism-json.js"; | ||
import "npm:prismjs@1.29.0/components/prism-bash.js"; | ||
import "npm:prismjs@1.29.0/components/prism-json5.js"; | ||
|
||
Prism.languages.jsonc = Prism.languages.json5; | ||
|
||
export default Prism; |
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,198 @@ | ||
import type { HrefResolver, ShortPath } from "@deno/doc"; | ||
import { dirname, join } from "@std/path"; | ||
import markdownit from "markdown-it"; | ||
import Prism from "../prism.ts"; | ||
|
||
import admonitionPlugin from "../markdown-it/admonition.ts"; | ||
import codeblockCopyPlugin from "../markdown-it/codeblock-copy.ts"; | ||
|
||
const titleOnlyAllowedTypes = new Set([ | ||
"inline", | ||
"paragraph_open", | ||
"paragraph_close", | ||
"heading_open", | ||
"heading_close", | ||
"text", | ||
"code_inline", | ||
"html_inline", | ||
"em_open", | ||
"em_close", | ||
"strong_open", | ||
"strong_close", | ||
"s_open", | ||
"s_close", | ||
"sup_open", | ||
"sup_close", | ||
"link_open", | ||
"link_close", | ||
"math_inline", | ||
"softbreak", | ||
"underline_open", | ||
"underline_close", | ||
]); | ||
|
||
function walkTitleTokens(tokens) { | ||
for (let i = 0; i < tokens.length; i++) { | ||
const token = tokens[i]; | ||
|
||
if (titleOnlyAllowedTypes.has(token.type)) { | ||
if (token.children) { | ||
walkTitleTokens(token.children); | ||
} | ||
} else { | ||
tokens.splice(i, 1); | ||
} | ||
} | ||
} | ||
|
||
function titleOnlyPlugin(md) { | ||
md.core.ruler.push("titleOnly", function titleOnly(state) { | ||
walkTitleTokens(state.tokens); | ||
|
||
const paragraphEnd = state.tokens.findIndex((token) => | ||
token.type === "paragraph_close" | ||
); | ||
|
||
if (paragraphEnd !== -1) { | ||
state.tokens.splice(paragraphEnd); | ||
} | ||
|
||
if ( | ||
state.tokens.length === 1 && state.tokens[0].type === "paragraph_open" | ||
) { | ||
state.tokens = []; | ||
} | ||
}); | ||
} | ||
|
||
function createAnchorizePlugin( | ||
anchorizer: (content: string, depthLevel: number) => string, | ||
) { | ||
return function anchorizePlugin(md) { | ||
md.core.ruler.push("anchorize", function anchorize(state) { | ||
const tokens = state.tokens; | ||
|
||
for (let i = 0; i < tokens.length; i++) { | ||
const token = tokens[i]; | ||
|
||
if (token.type === "heading_open") { | ||
const level = parseInt(token.tag.substring(1)); | ||
|
||
const nextToken = tokens[i + 1]; | ||
|
||
if (nextToken && nextToken.type === "inline") { | ||
const content = nextToken.children | ||
.filter((t) => t.type === "text" || t.type === "code_inline") | ||
.map((t) => t.content) | ||
.join(""); | ||
|
||
const id = anchorizer(content, level); | ||
token.attrSet("id", id); | ||
} | ||
} | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
function createRenderer( | ||
anchorizer: (content: string, depthLevel: number) => string, | ||
) { | ||
return markdownit({ | ||
html: true, | ||
linkify: true, | ||
langPrefix: "highlight notranslate language-", | ||
highlight(content: string, lang: string) { | ||
if (Prism.languages[lang]) { | ||
return Prism.highlight(content, Prism.languages[lang], lang); | ||
} else { | ||
return ""; | ||
} | ||
}, | ||
}) | ||
.disable("code") | ||
.use(admonitionPlugin) | ||
.use(codeblockCopyPlugin) | ||
.use(createAnchorizePlugin(anchorizer)); | ||
} | ||
|
||
export function renderMarkdown( | ||
md: string, | ||
titleOnly: boolean, | ||
_filePath: ShortPath | undefined, | ||
anchorizer: (content: string, depthLevel: number) => string, | ||
): string | undefined { | ||
const renderer = createRenderer(anchorizer); | ||
if (titleOnly) { | ||
const titleOnlyRenderer = renderer.use(titleOnlyPlugin); | ||
const parsed = titleOnlyRenderer.parse(md, {}); | ||
if (parsed.length === 0) { | ||
return undefined; | ||
} | ||
|
||
return `<div data-color-mode="dark" data-light-theme="light" data-dark-theme="dark" class="markdown-body markdown-summary">${ | ||
titleOnlyRenderer.renderer.render(parsed, titleOnlyRenderer.options, {}) | ||
}</div>`; | ||
} else { | ||
return `<div data-color-mode="dark" data-light-theme="light" data-dark-theme="dark" class="markdown-body">${ | ||
renderer.render(md) | ||
}</div>`; | ||
} | ||
} | ||
|
||
function strip(tokens) { | ||
let out = ""; | ||
|
||
for (const token of tokens) { | ||
if (token.type === "text" || token.type === "code_inline") { | ||
out += token.content; | ||
} else if (token.type === "fence") { | ||
out += token.content + "\n"; | ||
} else if (token.type === "softbreak") { | ||
out += "\n"; | ||
} else if ( | ||
token.type === "heading_close" || token.type === "paragraph_close" | ||
) { | ||
out += "\n\n"; | ||
} else if (token.children && token.children.length) { | ||
out += strip(token.children); | ||
} | ||
} | ||
|
||
return out; | ||
} | ||
|
||
export function stripMarkdown(md: string): string { | ||
const renderer = createRenderer(() => ""); | ||
const tokens = renderer.parse(md, {}); | ||
return strip(tokens); | ||
} | ||
|
||
export const hrefResolver: HrefResolver = { | ||
resolvePath(_current, _target, defaultResolve) { | ||
let path = defaultResolve(); | ||
|
||
if (path.endsWith("index.html")) { | ||
path = path.slice(0, -"index.html".length); | ||
} else if (path.endsWith(".html")) { | ||
path = path.slice(0, -".html".length); | ||
} | ||
|
||
return path; | ||
}, | ||
}; | ||
|
||
export async function writeFiles(root: string, files: Record<string, string>) { | ||
await Deno.remove(root, { recursive: true }); | ||
|
||
await Promise.all( | ||
Object.entries(files).map(async ([path, content]) => { | ||
const joined = join(root, path); | ||
|
||
await Deno.mkdir(dirname(joined), { recursive: true }); | ||
await Deno.writeTextFile(joined, content); | ||
}), | ||
); | ||
|
||
console.log(`Written ${Object.keys(files).length} files`); | ||
} |
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,33 @@ | ||
import { doc, generateHtml } from "@deno/doc"; | ||
import { | ||
hrefResolver, | ||
renderMarkdown, | ||
stripMarkdown, | ||
writeFiles, | ||
} from "./common.ts"; | ||
import categoryDocs from "./deno-categories.json" with { type: "json" }; | ||
|
||
const url = import.meta.resolve("./types/deno.d.ts"); | ||
|
||
console.log("Generating doc nodes..."); | ||
|
||
const nodes = await doc(url, { includeAll: true }); | ||
|
||
console.log("Generating html files..."); | ||
|
||
const files = await generateHtml({ [url]: nodes }, { | ||
packageName: "Deno", | ||
categoryDocs, | ||
disableSearch: true, | ||
hrefResolver, | ||
usageComposer: { | ||
singleMode: true, | ||
compose(_currentResolve, _usageToMd) { | ||
return new Map(); | ||
}, | ||
}, | ||
markdownRenderer: renderMarkdown, | ||
markdownStripper: stripMarkdown, | ||
}); | ||
|
||
await writeFiles("./gen/deno", files); |
File renamed without changes.
Oops, something went wrong.