chore(docs): add passing test results to documentation (#3541)
* chore(docs): add test results to documentation * chore: apply suggestion Co-authored-by: Tony Brix <tony@brix.ninja> * fix: lint --------- Co-authored-by: Tony Brix <tony@brix.ninja>
This commit is contained in:
parent
224d729bd8
commit
3da6e4712d
@ -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).
|
We actively support the features of the following [Markdown flavors](https://github.com/commonmark/CommonMark/wiki/Markdown-Flavors).
|
||||||
|
|
||||||
| Flavor | Version | Status |
|
<!--{{test-results-table}}-->
|
||||||
| :--------------------------------------------------------- | :------ | :----------------------------------------------------------------- |
|
|
||||||
| 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.
|
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.
|
||||||
|
|
||||||
|
@ -2,18 +2,41 @@
|
|||||||
import '../marked.min.js';
|
import '../marked.min.js';
|
||||||
import { promises } from 'fs';
|
import { promises } from 'fs';
|
||||||
import { join, dirname, parse, format } from 'path';
|
import { join, dirname, parse, format } from 'path';
|
||||||
|
import { fileURLToPath } from 'url';
|
||||||
import { markedHighlight } from 'marked-highlight';
|
import { markedHighlight } from 'marked-highlight';
|
||||||
import { HighlightJS } from 'highlight.js';
|
import { HighlightJS } from 'highlight.js';
|
||||||
import titleize from 'titleize';
|
import titleize from 'titleize';
|
||||||
|
import { getTests } from '@markedjs/testutils';
|
||||||
|
|
||||||
const { mkdir, rm, readdir, stat, readFile, writeFile, copyFile } = promises;
|
const { mkdir, rm, readdir, stat, readFile, writeFile, copyFile } = promises;
|
||||||
const { highlight, highlightAuto } = HighlightJS;
|
const { highlight, highlightAuto } = HighlightJS;
|
||||||
const cwd = process.cwd();
|
const cwd = process.cwd();
|
||||||
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
const __dirname = dirname(__filename);
|
||||||
const inputDir = join(cwd, 'docs');
|
const inputDir = join(cwd, 'docs');
|
||||||
const outputDir = join(cwd, 'public');
|
const outputDir = join(cwd, 'public');
|
||||||
const templateFile = join(inputDir, '_document.html');
|
const templateFile = join(inputDir, '_document.html');
|
||||||
const isUppercase = str => /[A-Z_]+/.test(str);
|
const isUppercase = str => /[A-Z_]+/.test(str);
|
||||||
const getTitle = str => str === 'INDEX' ? '' : titleize(str.replace(/_/g, ' ')) + ' - ';
|
const getTitle = str => str === 'INDEX' ? '' : titleize(str.replace(/_/g, ' ')) + ' - ';
|
||||||
|
function 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<details name="markdown-spec">
|
||||||
|
<summary>${name} (${(passing / total * 100).toFixed()}%)</summary>
|
||||||
|
${table}
|
||||||
|
</details>\n`;
|
||||||
|
}
|
||||||
|
|
||||||
const markedInstance = new marked.Marked(markedHighlight((code, language) => {
|
const markedInstance = new marked.Marked(markedHighlight((code, language) => {
|
||||||
if (!language) {
|
if (!language) {
|
||||||
return highlightAuto(code).value;
|
return highlightAuto(code).value;
|
||||||
@ -31,7 +54,16 @@ async function init() {
|
|||||||
await copyFile(join(cwd, 'marked.min.js'), join(outputDir, 'marked.min.js'));
|
await copyFile(join(cwd, 'marked.min.js'), join(outputDir, 'marked.min.js'));
|
||||||
const tmpl = await readFile(templateFile, 'utf8');
|
const tmpl = await readFile(templateFile, 'utf8');
|
||||||
console.log('Building markdown...');
|
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!');
|
console.log('Build complete!');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,7 +73,7 @@ const ignoredFiles = [
|
|||||||
join(cwd, 'docs', '_document.html'),
|
join(cwd, 'docs', '_document.html'),
|
||||||
];
|
];
|
||||||
|
|
||||||
async function build(currentDir, tmpl) {
|
async function build(currentDir, tmpl, testResultsTable) {
|
||||||
const files = await readdir(currentDir);
|
const files = await readdir(currentDir);
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
const filename = join(currentDir, file);
|
const filename = join(currentDir, file);
|
||||||
@ -56,7 +88,9 @@ async function build(currentDir, tmpl) {
|
|||||||
let html = await readFile(filename, 'utf8');
|
let html = await readFile(filename, 'utf8');
|
||||||
const parsed = parse(filename);
|
const parsed = parse(filename);
|
||||||
if (parsed.ext === '.md' && isUppercase(parsed.name)) {
|
if (parsed.ext === '.md' && isUppercase(parsed.name)) {
|
||||||
const mdHtml = markedInstance.parse(html);
|
const mdHtml = markedInstance.parse(
|
||||||
|
html.replace('<!--{{test-results-table}}-->', testResultsTable),
|
||||||
|
);
|
||||||
html = tmpl
|
html = tmpl
|
||||||
.replace('<!--{{title}}-->', getTitle(parsed.name))
|
.replace('<!--{{title}}-->', getTitle(parsed.name))
|
||||||
.replace('<!--{{content}}-->', mdHtml);
|
.replace('<!--{{content}}-->', mdHtml);
|
||||||
|
@ -112,6 +112,15 @@ pre code {
|
|||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
summary {
|
||||||
|
cursor: pointer;
|
||||||
|
padding: 3px 0px;
|
||||||
|
}
|
||||||
|
|
||||||
|
summary:hover {
|
||||||
|
color: #0366d6;
|
||||||
|
}
|
||||||
|
|
||||||
.div-copy {
|
.div-copy {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
top: 0;
|
top: 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user