@jxsuite/parser 0.34.0 → 0.35.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/package.json +4 -2
- package/src/md-html.ts +49 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jxsuite/parser",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.35.0",
|
|
4
4
|
"description": "Jx markdown parser and external class integration",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
".": "./src/md.ts",
|
|
18
18
|
"./content": "./src/content.ts",
|
|
19
19
|
"./markdown": "./src/markdown.ts",
|
|
20
|
+
"./md-html": "./src/md-html.ts",
|
|
20
21
|
"./csv": "./src/csv.ts",
|
|
21
22
|
"./serialize": "./src/serialize.ts",
|
|
22
23
|
"./ContentCollection.class.json": "./src/ContentCollection.class.json",
|
|
@@ -46,6 +47,7 @@
|
|
|
46
47
|
"hast-util-whitespace": "^3.0.0",
|
|
47
48
|
"mdast-util-to-string": "^4.0.0",
|
|
48
49
|
"property-information": "^7.2.0",
|
|
50
|
+
"rehype-sanitize": "^6.0.0",
|
|
49
51
|
"rehype-stringify": "^10.0.1",
|
|
50
52
|
"remark-directive": "^4.0.0",
|
|
51
53
|
"remark-frontmatter": "^5.0.0",
|
|
@@ -60,7 +62,7 @@
|
|
|
60
62
|
},
|
|
61
63
|
"devDependencies": {
|
|
62
64
|
"@happy-dom/global-registrator": "^20.10.6",
|
|
63
|
-
"@jxsuite/runtime": "^0.34.
|
|
65
|
+
"@jxsuite/runtime": "^0.34.1",
|
|
64
66
|
"@types/hast": "^3.0.4",
|
|
65
67
|
"@types/mdast": "^4.0.4"
|
|
66
68
|
}
|
package/src/md-html.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Md-html — browser-safe markdown → sanitized HTML string
|
|
3
|
+
*
|
|
4
|
+
* Small display pipeline for rendering untrusted markdown (e.g. LLM chat output) as
|
|
5
|
+
* HTML: remark-parse + remark-gfm → remark-rehype (raw HTML disabled) → rehype-sanitize
|
|
6
|
+
* (default schema: strips scripts, event handlers, javascript:/data: URLs) → stringify.
|
|
7
|
+
*
|
|
8
|
+
* Unlike `./md.ts` this module has no node-only imports, so it is safe for the studio
|
|
9
|
+
* bundle. It intentionally produces an HTML *string*, not Jx nodes — use
|
|
10
|
+
* `@jxsuite/parser/markdown` for markdown → Jx document transpilation.
|
|
11
|
+
*
|
|
12
|
+
* @module @jxsuite/parser/md-html
|
|
13
|
+
* @license MIT
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import rehypeSanitize from "rehype-sanitize";
|
|
17
|
+
import rehypeStringify from "rehype-stringify";
|
|
18
|
+
import remarkGfm from "remark-gfm";
|
|
19
|
+
import remarkParse from "remark-parse";
|
|
20
|
+
import remarkRehype from "remark-rehype";
|
|
21
|
+
import { unified } from "unified";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Minimal structural surface of the unified processor used here — same rationale as
|
|
25
|
+
* `UnifiedProcessor` in ./types.ts (oxlint sees the ESM-only unified types as `any`), plus
|
|
26
|
+
* `processSync` which the shared type doesn't carry.
|
|
27
|
+
*/
|
|
28
|
+
interface MdHtmlProcessor {
|
|
29
|
+
use: (plugin: unknown, ...options: unknown[]) => MdHtmlProcessor;
|
|
30
|
+
processSync: (source: string) => { toString: () => string };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const processor = (unified as unknown as () => MdHtmlProcessor)()
|
|
34
|
+
.use(remarkParse)
|
|
35
|
+
.use(remarkGfm)
|
|
36
|
+
.use(remarkRehype)
|
|
37
|
+
.use(rehypeSanitize)
|
|
38
|
+
.use(rehypeStringify);
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Convert markdown source to a sanitized HTML string (GFM tables/strikethrough/task lists
|
|
42
|
+
* supported). Raw HTML in the source is dropped, not passed through.
|
|
43
|
+
*
|
|
44
|
+
* @param {string} markdown
|
|
45
|
+
* @returns {string}
|
|
46
|
+
*/
|
|
47
|
+
export function markdownToHtml(markdown: string): string {
|
|
48
|
+
return String(processor.processSync(markdown));
|
|
49
|
+
}
|