marked-tree-to-html 1.0.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 +24 -0
- package/README.md +69 -0
- package/dist/index.cjs +86 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +18 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +59 -0
- package/dist/index.js.map +1 -0
- package/package.json +48 -0
- package/src/index.ts +76 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
|
2
|
+
|
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
4
|
+
distribute this software, either in source code form or as a compiled
|
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
|
6
|
+
means.
|
|
7
|
+
|
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
|
9
|
+
of this software dedicate any and all copyright interest in the
|
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
|
11
|
+
of the public at large and to the detriment of our heirs and
|
|
12
|
+
successors. We intend this dedication to be an overt act of
|
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
|
14
|
+
software under copyright law.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
For more information, please refer to <http://unlicense.org/>
|
package/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# marked-tree-to-html
|
|
2
|
+
|
|
3
|
+
Marked extension for rendering ASCII tree structures as HTML tables.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install marked-tree-to-html tree-to-html marked
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Or install from GitHub:
|
|
12
|
+
|
|
13
|
+
```json
|
|
14
|
+
{
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"marked-tree-to-html": "github:iafan/marked-tree-to-html",
|
|
17
|
+
"tree-to-html": "github:iafan/tree-to-html"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { marked } from 'marked'
|
|
26
|
+
import { treeExtension, renderTreeBlocks } from 'marked-tree-to-html'
|
|
27
|
+
|
|
28
|
+
// Register the extension
|
|
29
|
+
marked.use({ extensions: [treeExtension] })
|
|
30
|
+
|
|
31
|
+
// Parse markdown
|
|
32
|
+
const html = marked.parse(markdown)
|
|
33
|
+
|
|
34
|
+
// Insert into DOM
|
|
35
|
+
document.body.innerHTML = html
|
|
36
|
+
|
|
37
|
+
// Post-process to render tree structures
|
|
38
|
+
renderTreeBlocks()
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## How It Works
|
|
42
|
+
|
|
43
|
+
The extension intercepts:
|
|
44
|
+
1. Code blocks marked with ` ```tree `
|
|
45
|
+
2. Unmarked code blocks that contain ASCII tree characters (├, └, ─)
|
|
46
|
+
|
|
47
|
+
These are rendered as placeholders that `renderTreeBlocks()` converts to HTML tables.
|
|
48
|
+
|
|
49
|
+
## API
|
|
50
|
+
|
|
51
|
+
### `treeExtension`
|
|
52
|
+
|
|
53
|
+
Marked extension object. Use with `marked.use({ extensions: [treeExtension] })`.
|
|
54
|
+
|
|
55
|
+
### `renderTreeBlocks(): void`
|
|
56
|
+
|
|
57
|
+
Post-processes the DOM to convert tree placeholders to actual HTML tables.
|
|
58
|
+
|
|
59
|
+
### `isTreeStructure(code: string): boolean`
|
|
60
|
+
|
|
61
|
+
Check if a string contains ASCII tree characters.
|
|
62
|
+
|
|
63
|
+
## Styling
|
|
64
|
+
|
|
65
|
+
See [tree-to-html](https://github.com/iafan/tree-to-html) for CSS class documentation.
|
|
66
|
+
|
|
67
|
+
## License
|
|
68
|
+
|
|
69
|
+
This is free and unencumbered software released into the public domain (Unlicense).
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
isTreeStructure: () => isTreeStructure,
|
|
24
|
+
renderTreeBlocks: () => renderTreeBlocks,
|
|
25
|
+
treeExtension: () => treeExtension
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
var import_tree_to_html = require("tree-to-html");
|
|
29
|
+
function isTreeStructure(code) {
|
|
30
|
+
const hasBranch = code.includes("\u251C");
|
|
31
|
+
const hasCorner = code.includes("\u2514");
|
|
32
|
+
const hasDash = code.includes("\u2500");
|
|
33
|
+
return (hasBranch || hasCorner) && hasDash;
|
|
34
|
+
}
|
|
35
|
+
function renderTreeBlocks() {
|
|
36
|
+
const containers = document.querySelectorAll(".tree-container-raw");
|
|
37
|
+
for (const container of containers) {
|
|
38
|
+
const source = container.textContent || "";
|
|
39
|
+
if (!source.trim()) continue;
|
|
40
|
+
try {
|
|
41
|
+
const html = (0, import_tree_to_html.renderTree)(source);
|
|
42
|
+
container.outerHTML = html;
|
|
43
|
+
} catch (error) {
|
|
44
|
+
console.error("Failed to render tree:", error);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
var treeExtension = {
|
|
49
|
+
name: "tree",
|
|
50
|
+
level: "block",
|
|
51
|
+
start(src) {
|
|
52
|
+
const match = src.match(/^```(?:tree)?\n/);
|
|
53
|
+
return match?.index;
|
|
54
|
+
},
|
|
55
|
+
tokenizer(src) {
|
|
56
|
+
const treeMatch = src.match(/^```tree\n([\s\S]*?)\n```/);
|
|
57
|
+
if (treeMatch) {
|
|
58
|
+
return {
|
|
59
|
+
type: "tree",
|
|
60
|
+
raw: treeMatch[0],
|
|
61
|
+
text: treeMatch[1]
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
const codeMatch = src.match(/^```\n([\s\S]*?)\n```/);
|
|
65
|
+
if (codeMatch && isTreeStructure(codeMatch[1])) {
|
|
66
|
+
return {
|
|
67
|
+
type: "tree",
|
|
68
|
+
raw: codeMatch[0],
|
|
69
|
+
text: codeMatch[1]
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return void 0;
|
|
73
|
+
},
|
|
74
|
+
renderer(token) {
|
|
75
|
+
const escaped = token.text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
76
|
+
return `<div class="tree-container-raw">${escaped}</div>
|
|
77
|
+
`;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
81
|
+
0 && (module.exports = {
|
|
82
|
+
isTreeStructure,
|
|
83
|
+
renderTreeBlocks,
|
|
84
|
+
treeExtension
|
|
85
|
+
});
|
|
86
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { TokenizerExtension, RendererExtension } from 'marked'\nimport { renderTree } from 'tree-to-html'\n\n/**\n * Check if a code block contains ASCII tree characters.\n */\nexport function isTreeStructure(code: string): boolean {\n const hasBranch = code.includes('├')\n const hasCorner = code.includes('└')\n const hasDash = code.includes('─')\n return (hasBranch || hasCorner) && hasDash\n}\n\n/**\n * Post-process rendered HTML to convert tree containers to actual tree HTML.\n * Call this after marked.parse() completes and DOM is ready.\n */\nexport function renderTreeBlocks(): void {\n const containers = document.querySelectorAll<HTMLElement>('.tree-container-raw')\n\n for (const container of containers) {\n const source = container.textContent || ''\n if (!source.trim()) continue\n\n try {\n const html = renderTree(source)\n container.outerHTML = html\n } catch (error) {\n console.error('Failed to render tree:', error)\n }\n }\n}\n\n/**\n * Marked extension that intercepts code blocks with tree structure.\n * Use with: marked.use({ extensions: [treeExtension] })\n */\nexport const treeExtension: TokenizerExtension & RendererExtension = {\n name: 'tree',\n level: 'block',\n start(src: string) {\n // Look for code blocks that might contain tree structure\n const match = src.match(/^```(?:tree)?\\n/)\n return match?.index\n },\n tokenizer(src: string) {\n // Match code blocks: ```tree or ``` followed by tree content\n const treeMatch = src.match(/^```tree\\n([\\s\\S]*?)\\n```/)\n if (treeMatch) {\n return {\n type: 'tree',\n raw: treeMatch[0],\n text: treeMatch[1],\n }\n }\n\n // Match unmarked code blocks that contain tree characters\n const codeMatch = src.match(/^```\\n([\\s\\S]*?)\\n```/)\n if (codeMatch && isTreeStructure(codeMatch[1])) {\n return {\n type: 'tree',\n raw: codeMatch[0],\n text: codeMatch[1],\n }\n }\n\n return undefined\n },\n renderer(token) {\n const escaped = token.text\n .replace(/&/g, '&')\n .replace(/</g, '<')\n .replace(/>/g, '>')\n return `<div class=\"tree-container-raw\">${escaped}</div>\\n`\n },\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,0BAA2B;AAKpB,SAAS,gBAAgB,MAAuB;AACrD,QAAM,YAAY,KAAK,SAAS,QAAG;AACnC,QAAM,YAAY,KAAK,SAAS,QAAG;AACnC,QAAM,UAAU,KAAK,SAAS,QAAG;AACjC,UAAQ,aAAa,cAAc;AACrC;AAMO,SAAS,mBAAyB;AACvC,QAAM,aAAa,SAAS,iBAA8B,qBAAqB;AAE/E,aAAW,aAAa,YAAY;AAClC,UAAM,SAAS,UAAU,eAAe;AACxC,QAAI,CAAC,OAAO,KAAK,EAAG;AAEpB,QAAI;AACF,YAAM,WAAO,gCAAW,MAAM;AAC9B,gBAAU,YAAY;AAAA,IACxB,SAAS,OAAO;AACd,cAAQ,MAAM,0BAA0B,KAAK;AAAA,IAC/C;AAAA,EACF;AACF;AAMO,IAAM,gBAAwD;AAAA,EACnE,MAAM;AAAA,EACN,OAAO;AAAA,EACP,MAAM,KAAa;AAEjB,UAAM,QAAQ,IAAI,MAAM,iBAAiB;AACzC,WAAO,OAAO;AAAA,EAChB;AAAA,EACA,UAAU,KAAa;AAErB,UAAM,YAAY,IAAI,MAAM,2BAA2B;AACvD,QAAI,WAAW;AACb,aAAO;AAAA,QACL,MAAM;AAAA,QACN,KAAK,UAAU,CAAC;AAAA,QAChB,MAAM,UAAU,CAAC;AAAA,MACnB;AAAA,IACF;AAGA,UAAM,YAAY,IAAI,MAAM,uBAAuB;AACnD,QAAI,aAAa,gBAAgB,UAAU,CAAC,CAAC,GAAG;AAC9C,aAAO;AAAA,QACL,MAAM;AAAA,QACN,KAAK,UAAU,CAAC;AAAA,QAChB,MAAM,UAAU,CAAC;AAAA,MACnB;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA,EACA,SAAS,OAAO;AACd,UAAM,UAAU,MAAM,KACnB,QAAQ,MAAM,OAAO,EACrB,QAAQ,MAAM,MAAM,EACpB,QAAQ,MAAM,MAAM;AACvB,WAAO,mCAAmC,OAAO;AAAA;AAAA,EACnD;AACF;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { TokenizerExtension, RendererExtension } from 'marked';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Check if a code block contains ASCII tree characters.
|
|
5
|
+
*/
|
|
6
|
+
declare function isTreeStructure(code: string): boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Post-process rendered HTML to convert tree containers to actual tree HTML.
|
|
9
|
+
* Call this after marked.parse() completes and DOM is ready.
|
|
10
|
+
*/
|
|
11
|
+
declare function renderTreeBlocks(): void;
|
|
12
|
+
/**
|
|
13
|
+
* Marked extension that intercepts code blocks with tree structure.
|
|
14
|
+
* Use with: marked.use({ extensions: [treeExtension] })
|
|
15
|
+
*/
|
|
16
|
+
declare const treeExtension: TokenizerExtension & RendererExtension;
|
|
17
|
+
|
|
18
|
+
export { isTreeStructure, renderTreeBlocks, treeExtension };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { TokenizerExtension, RendererExtension } from 'marked';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Check if a code block contains ASCII tree characters.
|
|
5
|
+
*/
|
|
6
|
+
declare function isTreeStructure(code: string): boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Post-process rendered HTML to convert tree containers to actual tree HTML.
|
|
9
|
+
* Call this after marked.parse() completes and DOM is ready.
|
|
10
|
+
*/
|
|
11
|
+
declare function renderTreeBlocks(): void;
|
|
12
|
+
/**
|
|
13
|
+
* Marked extension that intercepts code blocks with tree structure.
|
|
14
|
+
* Use with: marked.use({ extensions: [treeExtension] })
|
|
15
|
+
*/
|
|
16
|
+
declare const treeExtension: TokenizerExtension & RendererExtension;
|
|
17
|
+
|
|
18
|
+
export { isTreeStructure, renderTreeBlocks, treeExtension };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { renderTree } from "tree-to-html";
|
|
3
|
+
function isTreeStructure(code) {
|
|
4
|
+
const hasBranch = code.includes("\u251C");
|
|
5
|
+
const hasCorner = code.includes("\u2514");
|
|
6
|
+
const hasDash = code.includes("\u2500");
|
|
7
|
+
return (hasBranch || hasCorner) && hasDash;
|
|
8
|
+
}
|
|
9
|
+
function renderTreeBlocks() {
|
|
10
|
+
const containers = document.querySelectorAll(".tree-container-raw");
|
|
11
|
+
for (const container of containers) {
|
|
12
|
+
const source = container.textContent || "";
|
|
13
|
+
if (!source.trim()) continue;
|
|
14
|
+
try {
|
|
15
|
+
const html = renderTree(source);
|
|
16
|
+
container.outerHTML = html;
|
|
17
|
+
} catch (error) {
|
|
18
|
+
console.error("Failed to render tree:", error);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
var treeExtension = {
|
|
23
|
+
name: "tree",
|
|
24
|
+
level: "block",
|
|
25
|
+
start(src) {
|
|
26
|
+
const match = src.match(/^```(?:tree)?\n/);
|
|
27
|
+
return match?.index;
|
|
28
|
+
},
|
|
29
|
+
tokenizer(src) {
|
|
30
|
+
const treeMatch = src.match(/^```tree\n([\s\S]*?)\n```/);
|
|
31
|
+
if (treeMatch) {
|
|
32
|
+
return {
|
|
33
|
+
type: "tree",
|
|
34
|
+
raw: treeMatch[0],
|
|
35
|
+
text: treeMatch[1]
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
const codeMatch = src.match(/^```\n([\s\S]*?)\n```/);
|
|
39
|
+
if (codeMatch && isTreeStructure(codeMatch[1])) {
|
|
40
|
+
return {
|
|
41
|
+
type: "tree",
|
|
42
|
+
raw: codeMatch[0],
|
|
43
|
+
text: codeMatch[1]
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
return void 0;
|
|
47
|
+
},
|
|
48
|
+
renderer(token) {
|
|
49
|
+
const escaped = token.text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
50
|
+
return `<div class="tree-container-raw">${escaped}</div>
|
|
51
|
+
`;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
export {
|
|
55
|
+
isTreeStructure,
|
|
56
|
+
renderTreeBlocks,
|
|
57
|
+
treeExtension
|
|
58
|
+
};
|
|
59
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { TokenizerExtension, RendererExtension } from 'marked'\nimport { renderTree } from 'tree-to-html'\n\n/**\n * Check if a code block contains ASCII tree characters.\n */\nexport function isTreeStructure(code: string): boolean {\n const hasBranch = code.includes('├')\n const hasCorner = code.includes('└')\n const hasDash = code.includes('─')\n return (hasBranch || hasCorner) && hasDash\n}\n\n/**\n * Post-process rendered HTML to convert tree containers to actual tree HTML.\n * Call this after marked.parse() completes and DOM is ready.\n */\nexport function renderTreeBlocks(): void {\n const containers = document.querySelectorAll<HTMLElement>('.tree-container-raw')\n\n for (const container of containers) {\n const source = container.textContent || ''\n if (!source.trim()) continue\n\n try {\n const html = renderTree(source)\n container.outerHTML = html\n } catch (error) {\n console.error('Failed to render tree:', error)\n }\n }\n}\n\n/**\n * Marked extension that intercepts code blocks with tree structure.\n * Use with: marked.use({ extensions: [treeExtension] })\n */\nexport const treeExtension: TokenizerExtension & RendererExtension = {\n name: 'tree',\n level: 'block',\n start(src: string) {\n // Look for code blocks that might contain tree structure\n const match = src.match(/^```(?:tree)?\\n/)\n return match?.index\n },\n tokenizer(src: string) {\n // Match code blocks: ```tree or ``` followed by tree content\n const treeMatch = src.match(/^```tree\\n([\\s\\S]*?)\\n```/)\n if (treeMatch) {\n return {\n type: 'tree',\n raw: treeMatch[0],\n text: treeMatch[1],\n }\n }\n\n // Match unmarked code blocks that contain tree characters\n const codeMatch = src.match(/^```\\n([\\s\\S]*?)\\n```/)\n if (codeMatch && isTreeStructure(codeMatch[1])) {\n return {\n type: 'tree',\n raw: codeMatch[0],\n text: codeMatch[1],\n }\n }\n\n return undefined\n },\n renderer(token) {\n const escaped = token.text\n .replace(/&/g, '&')\n .replace(/</g, '<')\n .replace(/>/g, '>')\n return `<div class=\"tree-container-raw\">${escaped}</div>\\n`\n },\n}\n"],"mappings":";AACA,SAAS,kBAAkB;AAKpB,SAAS,gBAAgB,MAAuB;AACrD,QAAM,YAAY,KAAK,SAAS,QAAG;AACnC,QAAM,YAAY,KAAK,SAAS,QAAG;AACnC,QAAM,UAAU,KAAK,SAAS,QAAG;AACjC,UAAQ,aAAa,cAAc;AACrC;AAMO,SAAS,mBAAyB;AACvC,QAAM,aAAa,SAAS,iBAA8B,qBAAqB;AAE/E,aAAW,aAAa,YAAY;AAClC,UAAM,SAAS,UAAU,eAAe;AACxC,QAAI,CAAC,OAAO,KAAK,EAAG;AAEpB,QAAI;AACF,YAAM,OAAO,WAAW,MAAM;AAC9B,gBAAU,YAAY;AAAA,IACxB,SAAS,OAAO;AACd,cAAQ,MAAM,0BAA0B,KAAK;AAAA,IAC/C;AAAA,EACF;AACF;AAMO,IAAM,gBAAwD;AAAA,EACnE,MAAM;AAAA,EACN,OAAO;AAAA,EACP,MAAM,KAAa;AAEjB,UAAM,QAAQ,IAAI,MAAM,iBAAiB;AACzC,WAAO,OAAO;AAAA,EAChB;AAAA,EACA,UAAU,KAAa;AAErB,UAAM,YAAY,IAAI,MAAM,2BAA2B;AACvD,QAAI,WAAW;AACb,aAAO;AAAA,QACL,MAAM;AAAA,QACN,KAAK,UAAU,CAAC;AAAA,QAChB,MAAM,UAAU,CAAC;AAAA,MACnB;AAAA,IACF;AAGA,UAAM,YAAY,IAAI,MAAM,uBAAuB;AACnD,QAAI,aAAa,gBAAgB,UAAU,CAAC,CAAC,GAAG;AAC9C,aAAO;AAAA,QACL,MAAM;AAAA,QACN,KAAK,UAAU,CAAC;AAAA,QAChB,MAAM,UAAU,CAAC;AAAA,MACnB;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA,EACA,SAAS,OAAO;AACd,UAAM,UAAU,MAAM,KACnB,QAAQ,MAAM,OAAO,EACrB,QAAQ,MAAM,MAAM,EACpB,QAAQ,MAAM,MAAM;AACvB,WAAO,mCAAmC,OAAO;AAAA;AAAA,EACnD;AACF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "marked-tree-to-html",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Marked extension for rendering ASCII tree structures as HTML",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"src"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsup",
|
|
22
|
+
"dev": "tsup --watch",
|
|
23
|
+
"typecheck": "tsc --noEmit"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"marked",
|
|
27
|
+
"tree",
|
|
28
|
+
"ascii",
|
|
29
|
+
"html",
|
|
30
|
+
"extension"
|
|
31
|
+
],
|
|
32
|
+
"author": "Igor Afanasyev",
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/iafan/marked-tree-to-html.git"
|
|
36
|
+
},
|
|
37
|
+
"license": "Unlicense",
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"marked": ">=4.0.0",
|
|
40
|
+
"tree-to-html": ">=1.0.0"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"marked": "^17.0.1",
|
|
44
|
+
"tree-to-html": "^1.0.0",
|
|
45
|
+
"tsup": "^8.5.0",
|
|
46
|
+
"typescript": "^5.9.3"
|
|
47
|
+
}
|
|
48
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { TokenizerExtension, RendererExtension } from 'marked'
|
|
2
|
+
import { renderTree } from 'tree-to-html'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Check if a code block contains ASCII tree characters.
|
|
6
|
+
*/
|
|
7
|
+
export function isTreeStructure(code: string): boolean {
|
|
8
|
+
const hasBranch = code.includes('├')
|
|
9
|
+
const hasCorner = code.includes('└')
|
|
10
|
+
const hasDash = code.includes('─')
|
|
11
|
+
return (hasBranch || hasCorner) && hasDash
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Post-process rendered HTML to convert tree containers to actual tree HTML.
|
|
16
|
+
* Call this after marked.parse() completes and DOM is ready.
|
|
17
|
+
*/
|
|
18
|
+
export function renderTreeBlocks(): void {
|
|
19
|
+
const containers = document.querySelectorAll<HTMLElement>('.tree-container-raw')
|
|
20
|
+
|
|
21
|
+
for (const container of containers) {
|
|
22
|
+
const source = container.textContent || ''
|
|
23
|
+
if (!source.trim()) continue
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
const html = renderTree(source)
|
|
27
|
+
container.outerHTML = html
|
|
28
|
+
} catch (error) {
|
|
29
|
+
console.error('Failed to render tree:', error)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Marked extension that intercepts code blocks with tree structure.
|
|
36
|
+
* Use with: marked.use({ extensions: [treeExtension] })
|
|
37
|
+
*/
|
|
38
|
+
export const treeExtension: TokenizerExtension & RendererExtension = {
|
|
39
|
+
name: 'tree',
|
|
40
|
+
level: 'block',
|
|
41
|
+
start(src: string) {
|
|
42
|
+
// Look for code blocks that might contain tree structure
|
|
43
|
+
const match = src.match(/^```(?:tree)?\n/)
|
|
44
|
+
return match?.index
|
|
45
|
+
},
|
|
46
|
+
tokenizer(src: string) {
|
|
47
|
+
// Match code blocks: ```tree or ``` followed by tree content
|
|
48
|
+
const treeMatch = src.match(/^```tree\n([\s\S]*?)\n```/)
|
|
49
|
+
if (treeMatch) {
|
|
50
|
+
return {
|
|
51
|
+
type: 'tree',
|
|
52
|
+
raw: treeMatch[0],
|
|
53
|
+
text: treeMatch[1],
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Match unmarked code blocks that contain tree characters
|
|
58
|
+
const codeMatch = src.match(/^```\n([\s\S]*?)\n```/)
|
|
59
|
+
if (codeMatch && isTreeStructure(codeMatch[1])) {
|
|
60
|
+
return {
|
|
61
|
+
type: 'tree',
|
|
62
|
+
raw: codeMatch[0],
|
|
63
|
+
text: codeMatch[1],
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return undefined
|
|
68
|
+
},
|
|
69
|
+
renderer(token) {
|
|
70
|
+
const escaped = token.text
|
|
71
|
+
.replace(/&/g, '&')
|
|
72
|
+
.replace(/</g, '<')
|
|
73
|
+
.replace(/>/g, '>')
|
|
74
|
+
return `<div class="tree-container-raw">${escaped}</div>\n`
|
|
75
|
+
},
|
|
76
|
+
}
|