@glyphjs/parser 0.1.0
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/LICENSE +21 -0
- package/README.md +45 -0
- package/dist/index.cjs +108 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +27 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +100 -0
- package/dist/index.js.map +1 -0
- package/package.json +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Glyph JS Contributors
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# @glyphjs/parser
|
|
2
|
+
|
|
3
|
+
Markdown parser with a remark plugin for `ui:` fenced code blocks.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @glyphjs/parser
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { parseGlyphMarkdown } from '@glyphjs/parser';
|
|
15
|
+
|
|
16
|
+
const markdown = `
|
|
17
|
+
# Hello
|
|
18
|
+
|
|
19
|
+
\`\`\`ui:graph
|
|
20
|
+
nodes:
|
|
21
|
+
- id: a
|
|
22
|
+
label: Node A
|
|
23
|
+
edges:
|
|
24
|
+
- from: a
|
|
25
|
+
to: b
|
|
26
|
+
\`\`\`
|
|
27
|
+
`;
|
|
28
|
+
|
|
29
|
+
const ast = parseGlyphMarkdown(markdown);
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Using the remark plugin directly
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { unified } from 'unified';
|
|
36
|
+
import remarkParse from 'remark-parse';
|
|
37
|
+
import { remarkGlyph } from '@glyphjs/parser';
|
|
38
|
+
|
|
39
|
+
const processor = unified().use(remarkParse).use(remarkGlyph);
|
|
40
|
+
const tree = processor.parse(markdown);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Docs
|
|
44
|
+
|
|
45
|
+
https://github.com/VledicFranco/glyphjs
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var yaml = require('yaml');
|
|
4
|
+
var unified = require('unified');
|
|
5
|
+
var remarkParse = require('remark-parse');
|
|
6
|
+
var remarkFrontmatter = require('remark-frontmatter');
|
|
7
|
+
|
|
8
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
|
+
|
|
10
|
+
var remarkParse__default = /*#__PURE__*/_interopDefault(remarkParse);
|
|
11
|
+
var remarkFrontmatter__default = /*#__PURE__*/_interopDefault(remarkFrontmatter);
|
|
12
|
+
|
|
13
|
+
// src/plugin.ts
|
|
14
|
+
var DEFAULT_POSITION = {
|
|
15
|
+
start: { line: 0, column: 0 },
|
|
16
|
+
end: { line: 0, column: 0 }
|
|
17
|
+
};
|
|
18
|
+
var remarkGlyph = function() {
|
|
19
|
+
return (tree) => {
|
|
20
|
+
transformTree(tree);
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
function isRawRef(value) {
|
|
24
|
+
if (typeof value !== "object" || value === null) return false;
|
|
25
|
+
const obj = value;
|
|
26
|
+
return typeof obj["target"] === "string";
|
|
27
|
+
}
|
|
28
|
+
function transformTree(node) {
|
|
29
|
+
if (!("children" in node) || !Array.isArray(node.children)) return;
|
|
30
|
+
for (let i = 0; i < node.children.length; i++) {
|
|
31
|
+
const child = node.children[i];
|
|
32
|
+
if (!child) continue;
|
|
33
|
+
if (child.type === "code") {
|
|
34
|
+
const codeNode = child;
|
|
35
|
+
if (codeNode.lang && codeNode.lang.startsWith("ui:")) {
|
|
36
|
+
const glyphBlock = codeNodeToGlyphUIBlock(codeNode);
|
|
37
|
+
node.children[i] = glyphBlock;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if ("children" in child && Array.isArray(child.children)) {
|
|
42
|
+
transformTree(child);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function codeNodeToGlyphUIBlock(node) {
|
|
47
|
+
const lang = node.lang ?? "";
|
|
48
|
+
const componentType = lang.slice(3);
|
|
49
|
+
const rawYaml = node.value;
|
|
50
|
+
let parsedData = null;
|
|
51
|
+
let yamlError;
|
|
52
|
+
let glyphId;
|
|
53
|
+
let refs;
|
|
54
|
+
try {
|
|
55
|
+
const parsed = yaml.parse(rawYaml);
|
|
56
|
+
if (parsed !== null && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
57
|
+
const data = parsed;
|
|
58
|
+
if ("glyph-id" in data && typeof data["glyph-id"] === "string") {
|
|
59
|
+
glyphId = data["glyph-id"];
|
|
60
|
+
delete data["glyph-id"];
|
|
61
|
+
}
|
|
62
|
+
if ("refs" in data && Array.isArray(data["refs"])) {
|
|
63
|
+
const rawRefs = data["refs"];
|
|
64
|
+
const validRefs = rawRefs.filter(isRawRef);
|
|
65
|
+
if (validRefs.length > 0) {
|
|
66
|
+
refs = validRefs;
|
|
67
|
+
}
|
|
68
|
+
delete data["refs"];
|
|
69
|
+
}
|
|
70
|
+
parsedData = data;
|
|
71
|
+
} else {
|
|
72
|
+
parsedData = null;
|
|
73
|
+
yamlError = "YAML payload must be an object (mapping), got " + typeof parsed;
|
|
74
|
+
}
|
|
75
|
+
} catch (err) {
|
|
76
|
+
parsedData = null;
|
|
77
|
+
yamlError = err instanceof Error ? err.message : String(err);
|
|
78
|
+
}
|
|
79
|
+
const position = node.position ?? DEFAULT_POSITION;
|
|
80
|
+
const block = {
|
|
81
|
+
type: "glyphUIBlock",
|
|
82
|
+
componentType,
|
|
83
|
+
rawYaml,
|
|
84
|
+
parsedData,
|
|
85
|
+
position
|
|
86
|
+
};
|
|
87
|
+
if (yamlError !== void 0) {
|
|
88
|
+
block.yamlError = yamlError;
|
|
89
|
+
}
|
|
90
|
+
if (glyphId !== void 0) {
|
|
91
|
+
block.glyphId = glyphId;
|
|
92
|
+
}
|
|
93
|
+
if (refs !== void 0) {
|
|
94
|
+
block.refs = refs;
|
|
95
|
+
}
|
|
96
|
+
return block;
|
|
97
|
+
}
|
|
98
|
+
function parseGlyphMarkdown(markdown) {
|
|
99
|
+
const processor = unified.unified().use(remarkParse__default.default).use(remarkFrontmatter__default.default, ["yaml"]).use(remarkGlyph);
|
|
100
|
+
const tree = processor.parse(markdown);
|
|
101
|
+
const result = processor.runSync(tree);
|
|
102
|
+
return result;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
exports.parseGlyphMarkdown = parseGlyphMarkdown;
|
|
106
|
+
exports.remarkGlyph = remarkGlyph;
|
|
107
|
+
//# sourceMappingURL=index.cjs.map
|
|
108
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/plugin.ts","../src/parse.ts"],"names":["parseYaml","unified","remarkParse","remarkFrontmatter"],"mappings":";;;;;;;;;;;;;AAMA,IAAM,gBAAA,GAAmC;AAAA,EACvC,KAAA,EAAO,EAAE,IAAA,EAAM,CAAA,EAAG,QAAQ,CAAA,EAAE;AAAA,EAC5B,GAAA,EAAK,EAAE,IAAA,EAAM,CAAA,EAAG,QAAQ,CAAA;AAC1B,CAAA;AAYO,IAAM,cAAqC,WAAY;AAC5D,EAAA,OAAO,CAAC,IAAA,KAAoB;AAC1B,IAAA,aAAA,CAAc,IAAI,CAAA;AAAA,EACpB,CAAA;AACF;AAKA,SAAS,SAAS,KAAA,EAAiC;AACjD,EAAA,IAAI,OAAO,KAAA,KAAU,QAAA,IAAY,KAAA,KAAU,MAAM,OAAO,KAAA;AACxD,EAAA,MAAM,GAAA,GAAM,KAAA;AACZ,EAAA,OAAO,OAAO,GAAA,CAAI,QAAQ,CAAA,KAAM,QAAA;AAClC;AAMA,SAAS,cAAc,IAAA,EAAgC;AACrD,EAAA,IAAI,EAAE,cAAc,IAAA,CAAA,IAAS,CAAC,MAAM,OAAA,CAAQ,IAAA,CAAK,QAAQ,CAAA,EAAG;AAE5D,EAAA,KAAA,IAAS,IAAI,CAAA,EAAG,CAAA,GAAI,IAAA,CAAK,QAAA,CAAS,QAAQ,CAAA,EAAA,EAAK;AAC7C,IAAA,MAAM,KAAA,GAAQ,IAAA,CAAK,QAAA,CAAS,CAAC,CAAA;AAC7B,IAAA,IAAI,CAAC,KAAA,EAAO;AAEZ,IAAA,IAAI,KAAA,CAAM,SAAS,MAAA,EAAQ;AACzB,MAAA,MAAM,QAAA,GAAW,KAAA;AACjB,MAAA,IAAI,SAAS,IAAA,IAAQ,QAA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AAM,MAAA,GAAS,SAAA,CAAU,OAAA,CAAQ,IAAI,CAAA;AACrC,EAAA,OAAO,MAAA;AACT","file":"index.cjs","sourcesContent":["import type { Root as MdastRoot, Code, Parent } from 'mdast';\nimport type { Plugin } from 'unified';\nimport type { GlyphUIBlock, RawRef, SourcePosition } from '@glyphjs/types';\nimport { parse as parseYaml } from 'yaml';\n\n/** Default position used when a code node has no position info. */\nconst DEFAULT_POSITION: SourcePosition = {\n start: { line: 0, column: 0 },\n end: { line: 0, column: 0 },\n};\n\n/**\n * Remark plugin that transforms `ui:` fenced code blocks into GlyphUIBlock AST nodes.\n *\n * How it works:\n * 1. Walk the MDAST tree looking for `code` nodes whose `lang` starts with \"ui:\"\n * 2. Parse the YAML payload from the code block's value\n * 3. Extract `glyph-id` and `refs` from the parsed data\n * 4. Replace the code node with a GlyphUIBlock node\n * 5. Standard Markdown nodes pass through unchanged\n */\nexport const remarkGlyph: Plugin<[], MdastRoot> = function () {\n return (tree: MdastRoot) => {\n transformTree(tree);\n };\n};\n\n/**\n * Check whether a value is a valid RawRef object.\n */\nfunction isRawRef(value: unknown): value is RawRef {\n if (typeof value !== 'object' || value === null) return false;\n const obj = value as Record<string, unknown>;\n return typeof obj['target'] === 'string';\n}\n\n/**\n * Recursively walk the MDAST tree and replace `ui:` code blocks\n * with GlyphUIBlock nodes in-place.\n */\nfunction transformTree(node: MdastRoot | Parent): void {\n if (!('children' in node) || !Array.isArray(node.children)) return;\n\n for (let i = 0; i < node.children.length; i++) {\n const child = node.children[i];\n if (!child) continue;\n\n if (child.type === 'code') {\n const codeNode = child as Code;\n if (codeNode.lang && codeNode.lang.startsWith('ui:')) {\n const glyphBlock = codeNodeToGlyphUIBlock(codeNode);\n // Replace the code node with the GlyphUIBlock node in the children array\n (node.children as unknown[])[i] = glyphBlock;\n continue;\n }\n }\n\n // Recurse into nodes that have children (e.g., blockquotes, lists, list items)\n if ('children' in child && Array.isArray((child as Parent).children)) {\n transformTree(child as Parent);\n }\n }\n}\n\n/**\n * Convert a `code` MDAST node with a `ui:` lang into a GlyphUIBlock AST node.\n * Precondition: `node.lang` is defined and starts with \"ui:\".\n */\nfunction codeNodeToGlyphUIBlock(node: Code): GlyphUIBlock {\n const lang = node.lang ?? '';\n const componentType = lang.slice(3); // Remove \"ui:\" prefix\n const rawYaml = node.value;\n\n let parsedData: Record<string, unknown> | null = null;\n let yamlError: string | undefined;\n let glyphId: string | undefined;\n let refs: RawRef[] | undefined;\n\n try {\n const parsed: unknown = parseYaml(rawYaml);\n\n if (parsed !== null && typeof parsed === 'object' && !Array.isArray(parsed)) {\n const data = parsed as Record<string, unknown>;\n\n // Extract glyph-id if present\n if ('glyph-id' in data && typeof data['glyph-id'] === 'string') {\n glyphId = data['glyph-id'];\n delete data['glyph-id'];\n }\n\n // Extract refs if present and is an array\n if ('refs' in data && Array.isArray(data['refs'])) {\n const rawRefs = data['refs'] as unknown[];\n const validRefs = rawRefs.filter(isRawRef);\n if (validRefs.length > 0) {\n refs = validRefs;\n }\n delete data['refs'];\n }\n\n parsedData = data;\n } else {\n // YAML parsed to a non-object (e.g., scalar or array at root)\n parsedData = null;\n yamlError = 'YAML payload must be an object (mapping), got ' + typeof parsed;\n }\n } catch (err: unknown) {\n parsedData = null;\n yamlError = err instanceof Error ? err.message : String(err);\n }\n\n const position: SourcePosition = node.position ?? DEFAULT_POSITION;\n\n const block: GlyphUIBlock = {\n type: 'glyphUIBlock',\n componentType,\n rawYaml,\n parsedData,\n position,\n };\n\n if (yamlError !== undefined) {\n block.yamlError = yamlError;\n }\n if (glyphId !== undefined) {\n block.glyphId = glyphId;\n }\n if (refs !== undefined) {\n block.refs = refs;\n }\n\n return block;\n}\n","import { unified } from 'unified';\nimport remarkParse from 'remark-parse';\nimport remarkFrontmatter from 'remark-frontmatter';\nimport type { GlyphRoot } from '@glyphjs/types';\nimport { remarkGlyph } from './plugin.js';\n\n/**\n * Parse a Glyph Markdown string into a GlyphRoot AST.\n *\n * Sets up a unified pipeline with:\n * 1. remark-parse — standard Markdown parsing\n * 2. remark-frontmatter — YAML frontmatter support\n * 3. remarkGlyph — transforms ui: fenced blocks into GlyphUIBlock nodes\n */\nexport function parseGlyphMarkdown(markdown: string): GlyphRoot {\n const processor = unified()\n .use(remarkParse)\n .use(remarkFrontmatter, ['yaml'])\n .use(remarkGlyph);\n\n const tree = processor.parse(markdown);\n const result = processor.runSync(tree);\n return result as unknown as GlyphRoot;\n}\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Root } from 'mdast';
|
|
2
|
+
import { Plugin } from 'unified';
|
|
3
|
+
import { GlyphRoot } from '@glyphjs/types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Remark plugin that transforms `ui:` fenced code blocks into GlyphUIBlock AST nodes.
|
|
7
|
+
*
|
|
8
|
+
* How it works:
|
|
9
|
+
* 1. Walk the MDAST tree looking for `code` nodes whose `lang` starts with "ui:"
|
|
10
|
+
* 2. Parse the YAML payload from the code block's value
|
|
11
|
+
* 3. Extract `glyph-id` and `refs` from the parsed data
|
|
12
|
+
* 4. Replace the code node with a GlyphUIBlock node
|
|
13
|
+
* 5. Standard Markdown nodes pass through unchanged
|
|
14
|
+
*/
|
|
15
|
+
declare const remarkGlyph: Plugin<[], Root>;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Parse a Glyph Markdown string into a GlyphRoot AST.
|
|
19
|
+
*
|
|
20
|
+
* Sets up a unified pipeline with:
|
|
21
|
+
* 1. remark-parse — standard Markdown parsing
|
|
22
|
+
* 2. remark-frontmatter — YAML frontmatter support
|
|
23
|
+
* 3. remarkGlyph — transforms ui: fenced blocks into GlyphUIBlock nodes
|
|
24
|
+
*/
|
|
25
|
+
declare function parseGlyphMarkdown(markdown: string): GlyphRoot;
|
|
26
|
+
|
|
27
|
+
export { parseGlyphMarkdown, remarkGlyph };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Root } from 'mdast';
|
|
2
|
+
import { Plugin } from 'unified';
|
|
3
|
+
import { GlyphRoot } from '@glyphjs/types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Remark plugin that transforms `ui:` fenced code blocks into GlyphUIBlock AST nodes.
|
|
7
|
+
*
|
|
8
|
+
* How it works:
|
|
9
|
+
* 1. Walk the MDAST tree looking for `code` nodes whose `lang` starts with "ui:"
|
|
10
|
+
* 2. Parse the YAML payload from the code block's value
|
|
11
|
+
* 3. Extract `glyph-id` and `refs` from the parsed data
|
|
12
|
+
* 4. Replace the code node with a GlyphUIBlock node
|
|
13
|
+
* 5. Standard Markdown nodes pass through unchanged
|
|
14
|
+
*/
|
|
15
|
+
declare const remarkGlyph: Plugin<[], Root>;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Parse a Glyph Markdown string into a GlyphRoot AST.
|
|
19
|
+
*
|
|
20
|
+
* Sets up a unified pipeline with:
|
|
21
|
+
* 1. remark-parse — standard Markdown parsing
|
|
22
|
+
* 2. remark-frontmatter — YAML frontmatter support
|
|
23
|
+
* 3. remarkGlyph — transforms ui: fenced blocks into GlyphUIBlock nodes
|
|
24
|
+
*/
|
|
25
|
+
declare function parseGlyphMarkdown(markdown: string): GlyphRoot;
|
|
26
|
+
|
|
27
|
+
export { parseGlyphMarkdown, remarkGlyph };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { parse } from 'yaml';
|
|
2
|
+
import { unified } from 'unified';
|
|
3
|
+
import remarkParse from 'remark-parse';
|
|
4
|
+
import remarkFrontmatter from 'remark-frontmatter';
|
|
5
|
+
|
|
6
|
+
// src/plugin.ts
|
|
7
|
+
var DEFAULT_POSITION = {
|
|
8
|
+
start: { line: 0, column: 0 },
|
|
9
|
+
end: { line: 0, column: 0 }
|
|
10
|
+
};
|
|
11
|
+
var remarkGlyph = function() {
|
|
12
|
+
return (tree) => {
|
|
13
|
+
transformTree(tree);
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
function isRawRef(value) {
|
|
17
|
+
if (typeof value !== "object" || value === null) return false;
|
|
18
|
+
const obj = value;
|
|
19
|
+
return typeof obj["target"] === "string";
|
|
20
|
+
}
|
|
21
|
+
function transformTree(node) {
|
|
22
|
+
if (!("children" in node) || !Array.isArray(node.children)) return;
|
|
23
|
+
for (let i = 0; i < node.children.length; i++) {
|
|
24
|
+
const child = node.children[i];
|
|
25
|
+
if (!child) continue;
|
|
26
|
+
if (child.type === "code") {
|
|
27
|
+
const codeNode = child;
|
|
28
|
+
if (codeNode.lang && codeNode.lang.startsWith("ui:")) {
|
|
29
|
+
const glyphBlock = codeNodeToGlyphUIBlock(codeNode);
|
|
30
|
+
node.children[i] = glyphBlock;
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if ("children" in child && Array.isArray(child.children)) {
|
|
35
|
+
transformTree(child);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function codeNodeToGlyphUIBlock(node) {
|
|
40
|
+
const lang = node.lang ?? "";
|
|
41
|
+
const componentType = lang.slice(3);
|
|
42
|
+
const rawYaml = node.value;
|
|
43
|
+
let parsedData = null;
|
|
44
|
+
let yamlError;
|
|
45
|
+
let glyphId;
|
|
46
|
+
let refs;
|
|
47
|
+
try {
|
|
48
|
+
const parsed = parse(rawYaml);
|
|
49
|
+
if (parsed !== null && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
50
|
+
const data = parsed;
|
|
51
|
+
if ("glyph-id" in data && typeof data["glyph-id"] === "string") {
|
|
52
|
+
glyphId = data["glyph-id"];
|
|
53
|
+
delete data["glyph-id"];
|
|
54
|
+
}
|
|
55
|
+
if ("refs" in data && Array.isArray(data["refs"])) {
|
|
56
|
+
const rawRefs = data["refs"];
|
|
57
|
+
const validRefs = rawRefs.filter(isRawRef);
|
|
58
|
+
if (validRefs.length > 0) {
|
|
59
|
+
refs = validRefs;
|
|
60
|
+
}
|
|
61
|
+
delete data["refs"];
|
|
62
|
+
}
|
|
63
|
+
parsedData = data;
|
|
64
|
+
} else {
|
|
65
|
+
parsedData = null;
|
|
66
|
+
yamlError = "YAML payload must be an object (mapping), got " + typeof parsed;
|
|
67
|
+
}
|
|
68
|
+
} catch (err) {
|
|
69
|
+
parsedData = null;
|
|
70
|
+
yamlError = err instanceof Error ? err.message : String(err);
|
|
71
|
+
}
|
|
72
|
+
const position = node.position ?? DEFAULT_POSITION;
|
|
73
|
+
const block = {
|
|
74
|
+
type: "glyphUIBlock",
|
|
75
|
+
componentType,
|
|
76
|
+
rawYaml,
|
|
77
|
+
parsedData,
|
|
78
|
+
position
|
|
79
|
+
};
|
|
80
|
+
if (yamlError !== void 0) {
|
|
81
|
+
block.yamlError = yamlError;
|
|
82
|
+
}
|
|
83
|
+
if (glyphId !== void 0) {
|
|
84
|
+
block.glyphId = glyphId;
|
|
85
|
+
}
|
|
86
|
+
if (refs !== void 0) {
|
|
87
|
+
block.refs = refs;
|
|
88
|
+
}
|
|
89
|
+
return block;
|
|
90
|
+
}
|
|
91
|
+
function parseGlyphMarkdown(markdown) {
|
|
92
|
+
const processor = unified().use(remarkParse).use(remarkFrontmatter, ["yaml"]).use(remarkGlyph);
|
|
93
|
+
const tree = processor.parse(markdown);
|
|
94
|
+
const result = processor.runSync(tree);
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export { parseGlyphMarkdown, remarkGlyph };
|
|
99
|
+
//# sourceMappingURL=index.js.map
|
|
100
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/plugin.ts","../src/parse.ts"],"names":["parseYaml"],"mappings":";;;;;;AAMA,IAAM,gBAAA,GAAmC;AAAA,EACvC,KAAA,EAAO,EAAE,IAAA,EAAM,CAAA,EAAG,QAAQ,CAAA,EAAE;AAAA,EAC5B,GAAA,EAAK,EAAE,IAAA,EAAM,CA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Y,GAAA,YAAe,KAAA,GAAQ,GAAA,CAAI,OAAA,GAAU,OAAO,GAAG,CAAA;AAAA,EAC7D;AAEA,EAAA,MAAM,QAAA,GAA2B,KAAK,QAAA,IAAY,gBAAA;AAElD,EAAA,MAAM,KAAA,GAAsB;AAAA,IAC1B,IAAA,EAAM,cAAA;AAAA,IACN,aAAA;AAAA,IACA,OAAA;AAAA,IACA,UAAA;AAAA,IACA;AAAA,GACF;AAEA,EAAA,IAAI,cAAc,MAAA,EAAW;AAC3B,IAAA,KAAA,CAAM,SAAA,GAAY,SAAA;AAAA,EACpB;AACA,EAAA,IAAI,YAAY,MAAA,EAAW;AACzB,IAAA,KAAA,CAAM,OAAA,GAAU,OAAA;AAAA,EAClB;AACA,EAAA,IAAI,SAAS,MAAA,EAAW;AACtB,IAAA,KAAA,CAAM,IAAA,GAAO,IAAA;AAAA,EACf;AAEA,EAAA,OAAO,KAAA;AACT;ACtHO,SAAS,mBAAmB,QAAA,EAA6B;AAC9D,EAAA,MAAM,SAAA,GAAY,OAAA,EAAQ,CACvB,GAAA,CAAI,WAAW,CAAA,CACf,GAAA,CAAI,iBAAA,EAAmB,CAAC,MAAM,CAAC,CAAA,CAC/B,IAAI,WAAW,CAAA;AAElB,EAAA,MAAM,IAAA,GAAO,SAAA,CAAU,KAAA,CAAM,QAAQ,CAAA;AACrC,EAAA,MAAM,MAAA,GAAS,SAAA,CAAU,OAAA,CAAQ,IAAI,CAAA;AACrC,EAAA,OAAO,MAAA;AACT","file":"index.js","sourcesContent":["import type { Root as MdastRoot, Code, Parent } from 'mdast';\nimport type { Plugin } from 'unified';\nimport type { GlyphUIBlock, RawRef, SourcePosition } from '@glyphjs/types';\nimport { parse as parseYaml } from 'yaml';\n\n/** Default position used when a code node has no position info. */\nconst DEFAULT_POSITION: SourcePosition = {\n start: { line: 0, column: 0 },\n end: { line: 0, column: 0 },\n};\n\n/**\n * Remark plugin that transforms `ui:` fenced code blocks into GlyphUIBlock AST nodes.\n *\n * How it works:\n * 1. Walk the MDAST tree looking for `code` nodes whose `lang` starts with \"ui:\"\n * 2. Parse the YAML payload from the code block's value\n * 3. Extract `glyph-id` and `refs` from the parsed data\n * 4. Replace the code node with a GlyphUIBlock node\n * 5. Standard Markdown nodes pass through unchanged\n */\nexport const remarkGlyph: Plugin<[], MdastRoot> = function () {\n return (tree: MdastRoot) => {\n transformTree(tree);\n };\n};\n\n/**\n * Check whether a value is a valid RawRef object.\n */\nfunction isRawRef(value: unknown): value is RawRef {\n if (typeof value !== 'object' || value === null) return false;\n const obj = value as Record<string, unknown>;\n return typeof obj['target'] === 'string';\n}\n\n/**\n * Recursively walk the MDAST tree and replace `ui:` code blocks\n * with GlyphUIBlock nodes in-place.\n */\nfunction transformTree(node: MdastRoot | Parent): void {\n if (!('children' in node) || !Array.isArray(node.children)) return;\n\n for (let i = 0; i < node.children.length; i++) {\n const child = node.children[i];\n if (!child) continue;\n\n if (child.type === 'code') {\n const codeNode = child as Code;\n if (codeNode.lang && codeNode.lang.startsWith('ui:')) {\n const glyphBlock = codeNodeToGlyphUIBlock(codeNode);\n // Replace the code node with the GlyphUIBlock node in the children array\n (node.children as unknown[])[i] = glyphBlock;\n continue;\n }\n }\n\n // Recurse into nodes that have children (e.g., blockquotes, lists, list items)\n if ('children' in child && Array.isArray((child as Parent).children)) {\n transformTree(child as Parent);\n }\n }\n}\n\n/**\n * Convert a `code` MDAST node with a `ui:` lang into a GlyphUIBlock AST node.\n * Precondition: `node.lang` is defined and starts with \"ui:\".\n */\nfunction codeNodeToGlyphUIBlock(node: Code): GlyphUIBlock {\n const lang = node.lang ?? '';\n const componentType = lang.slice(3); // Remove \"ui:\" prefix\n const rawYaml = node.value;\n\n let parsedData: Record<string, unknown> | null = null;\n let yamlError: string | undefined;\n let glyphId: string | undefined;\n let refs: RawRef[] | undefined;\n\n try {\n const parsed: unknown = parseYaml(rawYaml);\n\n if (parsed !== null && typeof parsed === 'object' && !Array.isArray(parsed)) {\n const data = parsed as Record<string, unknown>;\n\n // Extract glyph-id if present\n if ('glyph-id' in data && typeof data['glyph-id'] === 'string') {\n glyphId = data['glyph-id'];\n delete data['glyph-id'];\n }\n\n // Extract refs if present and is an array\n if ('refs' in data && Array.isArray(data['refs'])) {\n const rawRefs = data['refs'] as unknown[];\n const validRefs = rawRefs.filter(isRawRef);\n if (validRefs.length > 0) {\n refs = validRefs;\n }\n delete data['refs'];\n }\n\n parsedData = data;\n } else {\n // YAML parsed to a non-object (e.g., scalar or array at root)\n parsedData = null;\n yamlError = 'YAML payload must be an object (mapping), got ' + typeof parsed;\n }\n } catch (err: unknown) {\n parsedData = null;\n yamlError = err instanceof Error ? err.message : String(err);\n }\n\n const position: SourcePosition = node.position ?? DEFAULT_POSITION;\n\n const block: GlyphUIBlock = {\n type: 'glyphUIBlock',\n componentType,\n rawYaml,\n parsedData,\n position,\n };\n\n if (yamlError !== undefined) {\n block.yamlError = yamlError;\n }\n if (glyphId !== undefined) {\n block.glyphId = glyphId;\n }\n if (refs !== undefined) {\n block.refs = refs;\n }\n\n return block;\n}\n","import { unified } from 'unified';\nimport remarkParse from 'remark-parse';\nimport remarkFrontmatter from 'remark-frontmatter';\nimport type { GlyphRoot } from '@glyphjs/types';\nimport { remarkGlyph } from './plugin.js';\n\n/**\n * Parse a Glyph Markdown string into a GlyphRoot AST.\n *\n * Sets up a unified pipeline with:\n * 1. remark-parse — standard Markdown parsing\n * 2. remark-frontmatter — YAML frontmatter support\n * 3. remarkGlyph — transforms ui: fenced blocks into GlyphUIBlock nodes\n */\nexport function parseGlyphMarkdown(markdown: string): GlyphRoot {\n const processor = unified()\n .use(remarkParse)\n .use(remarkFrontmatter, ['yaml'])\n .use(remarkGlyph);\n\n const tree = processor.parse(markdown);\n const result = processor.runSync(tree);\n return result as unknown as GlyphRoot;\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@glyphjs/parser",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Markdown parser with remark plugin for ui: fenced blocks",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"LICENSE",
|
|
24
|
+
"README.md"
|
|
25
|
+
],
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"unified": "^11.0.5",
|
|
28
|
+
"remark-parse": "^11.0.0",
|
|
29
|
+
"remark-frontmatter": "^5.0.0",
|
|
30
|
+
"yaml": "^2.7.0",
|
|
31
|
+
"@glyphjs/types": "0.1.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@types/mdast": "^4.0.4",
|
|
35
|
+
"@types/unist": "^3.0.3"
|
|
36
|
+
},
|
|
37
|
+
"sideEffects": false,
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"author": "Vledic Franco",
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "https://github.com/VledicFranco/glyphjs.git",
|
|
43
|
+
"directory": "packages/parser"
|
|
44
|
+
},
|
|
45
|
+
"homepage": "https://github.com/VledicFranco/glyphjs#readme",
|
|
46
|
+
"bugs": "https://github.com/VledicFranco/glyphjs/issues",
|
|
47
|
+
"keywords": [
|
|
48
|
+
"glyph",
|
|
49
|
+
"glyphjs",
|
|
50
|
+
"markdown",
|
|
51
|
+
"react",
|
|
52
|
+
"ui-components",
|
|
53
|
+
"interactive-documents",
|
|
54
|
+
"remark",
|
|
55
|
+
"parser",
|
|
56
|
+
"unified",
|
|
57
|
+
"ast"
|
|
58
|
+
],
|
|
59
|
+
"scripts": {
|
|
60
|
+
"build": "tsup",
|
|
61
|
+
"typecheck": "tsc --noEmit",
|
|
62
|
+
"test": "vitest run",
|
|
63
|
+
"lint": "eslint src/",
|
|
64
|
+
"clean": "rm -rf dist .turbo"
|
|
65
|
+
}
|
|
66
|
+
}
|