build(docs): handle YAML edgecases in markdown
Our current method of adding a entry using `+=` is not safe in YAML, since it's valid to make a YAML object entirely in JSON, see https://github.com/mermaid-js/mermaid/pull/4640#discussion_r1265133463 We're already using `js-yaml` elsewhere in this file, so we may as well use it for parsing/stringifying. Reported-by: Remco Haszing <remcohaszing@gmail.com>
This commit is contained in:
parent
af9b3f77cb
commit
4d2d790cc8
|
@ -30,10 +30,19 @@
|
||||||
* @todo Write a test file for this. (Will need to be able to deal .mts file. Jest has trouble with
|
* @todo Write a test file for this. (Will need to be able to deal .mts file. Jest has trouble with
|
||||||
* it.)
|
* it.)
|
||||||
*/
|
*/
|
||||||
|
// @ts-ignore: we're importing internal jsonschema2md functions
|
||||||
|
import { default as schemaLoader } from '@adobe/jsonschema2md/lib/schemaProxy.js';
|
||||||
|
// @ts-ignore: we're importing internal jsonschema2md functions
|
||||||
|
import { default as traverseSchemas } from '@adobe/jsonschema2md/lib/traverseSchema.js';
|
||||||
|
// @ts-ignore: we're importing internal jsonschema2md functions
|
||||||
|
import { default as buildMarkdownFromSchema } from '@adobe/jsonschema2md/lib/markdownBuilder.js';
|
||||||
|
// @ts-ignore: we're importing internal jsonschema2md functions
|
||||||
|
import { default as jsonSchemaReadmeBuilder } from '@adobe/jsonschema2md/lib/readmeBuilder.js';
|
||||||
import { readFileSync, writeFileSync, mkdirSync, existsSync, rmSync, rmdirSync } from 'fs';
|
import { readFileSync, writeFileSync, mkdirSync, existsSync, rmSync, rmdirSync } from 'fs';
|
||||||
import { exec } from 'child_process';
|
import { exec } from 'child_process';
|
||||||
import { globby } from 'globby';
|
import { globby } from 'globby';
|
||||||
import { JSDOM } from 'jsdom';
|
import { JSDOM } from 'jsdom';
|
||||||
|
import { dump, load, JSON_SCHEMA } from 'js-yaml';
|
||||||
import type { Code, ListItem, Root, Text, YAML } from 'mdast';
|
import type { Code, ListItem, Root, Text, YAML } from 'mdast';
|
||||||
import { posix, dirname, relative, join } from 'path';
|
import { posix, dirname, relative, join } from 'path';
|
||||||
import prettier from 'prettier';
|
import prettier from 'prettier';
|
||||||
|
@ -286,10 +295,12 @@ export function transformMarkdownAst({
|
||||||
astWithTransformedBlocks.children.unshift(yamlFrontMatter);
|
astWithTransformedBlocks.children.unshift(yamlFrontMatter);
|
||||||
}
|
}
|
||||||
const filePathFromRoot = posix.join('packages/mermaid', originalFilename);
|
const filePathFromRoot = posix.join('packages/mermaid', originalFilename);
|
||||||
// TODO, should we replace this with proper YAML parsing?
|
yamlFrontMatter.value = dump({
|
||||||
yamlFrontMatter.value += `\neditLink: ${JSON.stringify(
|
...(load(yamlFrontMatter.value, { schema: JSON_SCHEMA }) as
|
||||||
`https://github.com/mermaid-js/mermaid/edit/develop/${filePathFromRoot}`
|
| Record<string, unknown>
|
||||||
)}`;
|
| undefined),
|
||||||
|
editLink: `https://github.com/mermaid-js/mermaid/edit/develop/${filePathFromRoot}`,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (removeYAML) {
|
if (removeYAML) {
|
||||||
|
@ -346,16 +357,6 @@ const transformMarkdown = (file: string) => {
|
||||||
copyTransformedContents(file, !verifyOnly, formatted);
|
copyTransformedContents(file, !verifyOnly, formatted);
|
||||||
};
|
};
|
||||||
|
|
||||||
import { load, JSON_SCHEMA } from 'js-yaml';
|
|
||||||
// @ts-ignore: we're importing internal jsonschema2md functions
|
|
||||||
import { default as schemaLoader } from '@adobe/jsonschema2md/lib/schemaProxy.js';
|
|
||||||
// @ts-ignore: we're importing internal jsonschema2md functions
|
|
||||||
import { default as traverseSchemas } from '@adobe/jsonschema2md/lib/traverseSchema.js';
|
|
||||||
// @ts-ignore: we're importing internal jsonschema2md functions
|
|
||||||
import { default as buildMarkdownFromSchema } from '@adobe/jsonschema2md/lib/markdownBuilder.js';
|
|
||||||
// @ts-ignore: we're importing internal jsonschema2md functions
|
|
||||||
import { default as jsonSchemaReadmeBuilder } from '@adobe/jsonschema2md/lib/readmeBuilder.js';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transforms the given JSON Schema into Markdown documentation
|
* Transforms the given JSON Schema into Markdown documentation
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -119,7 +119,9 @@ This Markdown should be kept.
|
||||||
).toString();
|
).toString();
|
||||||
expect(withYaml).toEqual(`---
|
expect(withYaml).toEqual(`---
|
||||||
title: Flowcharts Syntax
|
title: Flowcharts Syntax
|
||||||
editLink: "https://github.com/mermaid-js/mermaid/edit/develop/packages/mermaid/example-input-filename.md"
|
editLink: >-
|
||||||
|
https://github.com/mermaid-js/mermaid/edit/develop/packages/mermaid/example-input-filename.md
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
This Markdown should be kept.
|
This Markdown should be kept.
|
||||||
|
|
Loading…
Reference in New Issue