@pairdraw/mermaid 0.0.0-stage → 0.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/source.ts ADDED
@@ -0,0 +1,51 @@
1
+ import * as v from 'valibot'
2
+ import { parse as parseYAML } from 'yaml'
3
+
4
+ import { cleanupComments } from './upstream/comments'
5
+ import { cleanupText, encodeEntities, removeDirectives } from './upstream/preprocess'
6
+ import { matchFrontMatter } from './upstream/regexes'
7
+
8
+ /** Mermaid source as a diagram's grammar reads it, and the title its front matter gives. */
9
+ export interface PreparedSource {
10
+ code: string
11
+ title?: string
12
+ }
13
+
14
+ /** The front matter Pairdraw reads: the title, which YAML may give as a number or a flag. */
15
+ const frontMatterSchema = v.looseObject({
16
+ title: v.optional(v.pipe(v.union([v.string(), v.number(), v.boolean()]), v.toString()))
17
+ })
18
+
19
+ function frontMatterTitle(body: string): string | undefined {
20
+ try {
21
+ const parsed = v.safeParse(frontMatterSchema, parseYAML(body))
22
+ return parsed.success ? parsed.output.title : undefined
23
+ } catch {
24
+ return undefined
25
+ }
26
+ }
27
+
28
+ /**
29
+ * Prepares Mermaid source as Mermaid does before its grammar reads it (`preprocessDiagram`, then
30
+ * `Diagram.fromText`), with the functions copied from the pinned release: line endings and HTML
31
+ * attributes cleaned up, front matter, directives, and comments taken out, and `#name;` entities
32
+ * hidden from the grammar, which reads `;` as a statement break.
33
+ */
34
+ export function prepareSource(text: string): PreparedSource {
35
+ let code = cleanupText(text)
36
+ let title: string | undefined
37
+ const frontMatter = matchFrontMatter(code)
38
+ if (frontMatter) {
39
+ // Mermaid dedents front matter by its fences' indent before reading it.
40
+ const { indent } = frontMatter
41
+ const body = indent
42
+ ? frontMatter.body
43
+ .split('\n')
44
+ .map((line) => (line.startsWith(indent) ? line.slice(indent.length) : line))
45
+ .join('\n')
46
+ : frontMatter.body
47
+ title = frontMatterTitle(body)
48
+ code = code.slice(frontMatter.length)
49
+ }
50
+ return { code: `${encodeEntities(cleanupComments(removeDirectives(code)))}\n`, title }
51
+ }
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 - 2022 Knut Sveidqvist
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,13 @@
1
+ // @ts-nocheck
2
+ // Copied by `bun run generate:mermaid-sources` from mermaid@12.2.0
3
+ // (https://raw.githubusercontent.com/mermaid-js/mermaid/1fb7869866dd619168d840291c9c54b4f129758b/packages/mermaid/src/diagram-api/comments.ts)
4
+ // Copyright (c) 2014 - 2022 Knut Sveidqvist. MIT License; see MERMAID-LICENSE.txt.
5
+ // Do not edit: change the pinned release in tools/generate/mermaid-sources and regenerate.
6
+ /**
7
+ * Remove all lines starting with `%%` from the text that don't contain a `%%{`
8
+ * @param text - The text to remove comments from
9
+ * @returns cleaned text
10
+ */
11
+ export const cleanupComments = (text: string): string => {
12
+ return text.replace(/^\s*%%(?!{)[^\n]+\n?/gm, '').trimStart();
13
+ };
@@ -0,0 +1,8 @@
1
+ /** Mermaid's flowchart parser, generated from `flow.jison`; see `flow.js`. */
2
+ interface GeneratedParser {
3
+ yy: object
4
+ parse(text: string): unknown
5
+ }
6
+
7
+ declare const parser: GeneratedParser
8
+ export default parser