From 2e1df785f56b8108911bf03fbec6828442c44a8b Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 23 Nov 2024 13:23:15 -0500 Subject: [PATCH 1/5] chore(docs): add dynamic test results to documentation --- docs/INDEX.md | 6 +----- docs/build.js | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/docs/INDEX.md b/docs/INDEX.md index ba4b7fd662..36da026e14 100644 --- a/docs/INDEX.md +++ b/docs/INDEX.md @@ -143,11 +143,7 @@ Marked offers [advanced configurations](/using_advanced) and [extensibility](/us We actively support the features of the following [Markdown flavors](https://github.com/commonmark/CommonMark/wiki/Markdown-Flavors). -| Flavor | Version | Status | -| :--------------------------------------------------------- | :------ | :----------------------------------------------------------------- | -| The original markdown.pl | -- | | -| [CommonMark](http://spec.commonmark.org/0.31.2/) | 0.31 | [Work in progress](https://github.com/markedjs/marked/issues/1202) | -| [GitHub Flavored Markdown](https://github.github.com/gfm/) | 0.29 | [Work in progress](https://github.com/markedjs/marked/issues/1202) | + By supporting the above Markdown flavors, it's possible that Marked can help you use other flavors as well; however, these are not actively supported by the community. diff --git a/docs/build.js b/docs/build.js index aa11ae1c85..19cceaecdb 100644 --- a/docs/build.js +++ b/docs/build.js @@ -2,18 +2,41 @@ import '../marked.min.js'; import { promises } from 'fs'; import { join, dirname, parse, format } from 'path'; +import { fileURLToPath } from 'url'; import { markedHighlight } from 'marked-highlight'; import { HighlightJS } from 'highlight.js'; import titleize from 'titleize'; +import { getTests } from '@markedjs/testutils'; const { mkdir, rm, readdir, stat, readFile, writeFile, copyFile } = promises; const { highlight, highlightAuto } = HighlightJS; const cwd = process.cwd(); +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); const inputDir = join(cwd, 'docs'); const outputDir = join(cwd, 'public'); const templateFile = join(inputDir, '_document.html'); const isUppercase = str => /[A-Z_]+/.test(str); const getTitle = str => str === 'INDEX' ? '' : titleize(str.replace(/_/g, ' ')) + ' - '; +const convertTestsToTable = (name, tests) => { + let total = 0; + let passing = 0; + let table = '\n| Section | Passing | Percent |\n'; + table += '|:--------|:--------|--------:|\n'; + for (const [key, value] of Object.entries(tests)) { + total += value.total; + passing += value.pass; + table += ` | ${key}`; + table += ` | ${(value.pass)} of ${(value.total)}`; + table += ` | ${((value.pass) / value.total * 100).toFixed()}%`; + table += ' |\n'; + } + return `\n
+ ${name} (${(passing / total * 100).toFixed()}%) + ${table} +
\n`; +}; + const markedInstance = new marked.Marked(markedHighlight((code, language) => { if (!language) { return highlightAuto(code).value; @@ -31,7 +54,16 @@ async function init() { await copyFile(join(cwd, 'marked.min.js'), join(outputDir, 'marked.min.js')); const tmpl = await readFile(templateFile, 'utf8'); console.log('Building markdown...'); - await build(inputDir, tmpl); + const [original, commonmark, gfm] = await getTests([ + join(__dirname, '../test/specs/original'), + join(__dirname, '../test/specs/commonmark'), + join(__dirname, '../test/specs/gfm'), + ]); + const testResultsTable = + convertTestsToTable('Markdown 1.0', original) + + convertTestsToTable('CommonMark 0.31', commonmark) + + convertTestsToTable('GitHub Flavored Markdown 0.29', gfm); + await build(inputDir, tmpl, testResultsTable); console.log('Build complete!'); } @@ -41,7 +73,7 @@ const ignoredFiles = [ join(cwd, 'docs', '_document.html'), ]; -async function build(currentDir, tmpl) { +async function build(currentDir, tmpl, testResultsTable) { const files = await readdir(currentDir); for (const file of files) { const filename = join(currentDir, file); @@ -56,7 +88,9 @@ async function build(currentDir, tmpl) { let html = await readFile(filename, 'utf8'); const parsed = parse(filename); if (parsed.ext === '.md' && isUppercase(parsed.name)) { - const mdHtml = markedInstance.parse(html); + const mdHtml = markedInstance.parse( + html.replace('', testResultsTable) + ); html = tmpl .replace('', getTitle(parsed.name)) .replace('', mdHtml); From a89d1a99d232374d9b118dc2436c996be87b42f8 Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 23 Nov 2024 13:27:31 -0500 Subject: [PATCH 2/5] chore: lint --- docs/build.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/build.js b/docs/build.js index 19cceaecdb..c8b615c286 100644 --- a/docs/build.js +++ b/docs/build.js @@ -89,7 +89,7 @@ async function build(currentDir, tmpl, testResultsTable) { const parsed = parse(filename); if (parsed.ext === '.md' && isUppercase(parsed.name)) { const mdHtml = markedInstance.parse( - html.replace('', testResultsTable) + html.replace('', testResultsTable), ); html = tmpl .replace('', getTitle(parsed.name)) From caf90e1177004b3e4474811a12030289ed5a9e43 Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 23 Nov 2024 13:33:15 -0500 Subject: [PATCH 3/5] chore: style `summary` --- docs/css/style.css | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/css/style.css b/docs/css/style.css index 2398e0af36..eabe39f5bd 100644 --- a/docs/css/style.css +++ b/docs/css/style.css @@ -112,6 +112,15 @@ pre code { border-radius: 3px; } +summary { + cursor: pointer; + padding: 3px 0px; +} + +summary:hover { + color: #0366d6; +} + .div-copy { position: absolute; top: 0; From e0b79f2d7d63248d35886117aab09093a9f96b21 Mon Sep 17 00:00:00 2001 From: Steven Date: Sun, 24 Nov 2024 14:55:03 -0500 Subject: [PATCH 4/5] Apply suggestion Co-authored-by: Tony Brix --- docs/build.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/build.js b/docs/build.js index c8b615c286..fc500bd929 100644 --- a/docs/build.js +++ b/docs/build.js @@ -18,7 +18,7 @@ const outputDir = join(cwd, 'public'); const templateFile = join(inputDir, '_document.html'); const isUppercase = str => /[A-Z_]+/.test(str); const getTitle = str => str === 'INDEX' ? '' : titleize(str.replace(/_/g, ' ')) + ' - '; -const convertTestsToTable = (name, tests) => { +function convertTestsToTable(name, tests) { let total = 0; let passing = 0; let table = '\n| Section | Passing | Percent |\n'; From 941504e20af814e5f5cdf6b0823682c35ef47387 Mon Sep 17 00:00:00 2001 From: Steven Date: Sun, 24 Nov 2024 15:27:59 -0500 Subject: [PATCH 5/5] fix: lint --- docs/build.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/build.js b/docs/build.js index fc500bd929..d39e9eb7f9 100644 --- a/docs/build.js +++ b/docs/build.js @@ -35,7 +35,7 @@ function convertTestsToTable(name, tests) { ${name} (${(passing / total * 100).toFixed()}%) ${table} \n`; -}; +} const markedInstance = new marked.Marked(markedHighlight((code, language) => { if (!language) {