shiki-transformer-fold 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 +74 -0
- package/dist/index.d.mts +43 -0
- package/dist/index.mjs +165 -0
- package/package.json +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-present - Julien Huang
|
|
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,74 @@
|
|
|
1
|
+
# shiki-transformer-fold
|
|
2
|
+
|
|
3
|
+
<!-- automd:badges color=yellow -->
|
|
4
|
+
|
|
5
|
+
[](https://npmjs.com/package/shiki-transformer-fold)
|
|
6
|
+
[](https://npm.chart.dev/shiki-transformer-fold)
|
|
7
|
+
|
|
8
|
+
<!-- /automd -->
|
|
9
|
+
|
|
10
|
+
Code folding support for shiki.
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
Install the package:
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
# ✨ Auto-detect (supports npm, yarn, pnpm, deno and bun)
|
|
18
|
+
npx nypm install shiki-transformer-fold
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Add the transformer
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { codeToHtml } from "shiki";
|
|
25
|
+
import {
|
|
26
|
+
attachFoldToggleListener,
|
|
27
|
+
transformerRenderHtmlFold,
|
|
28
|
+
} from "shiki-transformer-fold";
|
|
29
|
+
|
|
30
|
+
const html = await codeToHtml(code, {
|
|
31
|
+
lang: "html",
|
|
32
|
+
theme: "nord",
|
|
33
|
+
transformers: [transformerRenderHtmlFold()],
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// attach listeners to allow opening and closing the rendered code
|
|
37
|
+
// need to be called only once
|
|
38
|
+
attachFoldToggleListener();
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Development
|
|
42
|
+
|
|
43
|
+
<details>
|
|
44
|
+
|
|
45
|
+
<summary>local development</summary>
|
|
46
|
+
|
|
47
|
+
- Clone this repository
|
|
48
|
+
- Install latest LTS version of [Node.js](https://nodejs.org/en/)
|
|
49
|
+
- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable`
|
|
50
|
+
- Install dependencies using `pnpm install`
|
|
51
|
+
- Run interactive tests using `pnpm dev`
|
|
52
|
+
|
|
53
|
+
</details>
|
|
54
|
+
|
|
55
|
+
## License
|
|
56
|
+
|
|
57
|
+
<!-- automd:contributors license=MIT -->
|
|
58
|
+
|
|
59
|
+
Published under the [MIT](https://github.com/huang-julien/shiki-transformer-fold/blob/main/LICENSE) license.
|
|
60
|
+
Made by [community](https://github.com/huang-julien/shiki-transformer-fold/graphs/contributors) 💛
|
|
61
|
+
<br><br>
|
|
62
|
+
<a href="https://github.com/huang-julien/shiki-transformer-fold/graphs/contributors">
|
|
63
|
+
<img src="https://contrib.rocks/image?repo=huang-julien/shiki-transformer-fold" />
|
|
64
|
+
</a>
|
|
65
|
+
|
|
66
|
+
<!-- /automd -->
|
|
67
|
+
|
|
68
|
+
<!-- automd:with-automd -->
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
_🤖 auto updated with [automd](https://automd.unjs.io)_
|
|
73
|
+
|
|
74
|
+
<!-- /automd -->
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ShikiTransformer } from "shiki";
|
|
2
|
+
|
|
3
|
+
//#region src/html.d.ts
|
|
4
|
+
interface TransformerHtmlFoldOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Custom class prefix for fold elements
|
|
7
|
+
* @default 'shiki-fold'
|
|
8
|
+
*/
|
|
9
|
+
classPrefix?: string;
|
|
10
|
+
}
|
|
11
|
+
interface FoldRegion {
|
|
12
|
+
startLine: number;
|
|
13
|
+
endLine: number;
|
|
14
|
+
}
|
|
15
|
+
declare module "shiki" {
|
|
16
|
+
interface ShikiTransformerContextMeta {
|
|
17
|
+
/**
|
|
18
|
+
* Metadata added by transformerHtmlFold fromshiki-transformer-fold
|
|
19
|
+
*/
|
|
20
|
+
transformerHtmlFold: {
|
|
21
|
+
/** Map of foldable regions detected in the code */
|
|
22
|
+
foldRegions: FoldRegion[];
|
|
23
|
+
/** Map of foldable regions detected in the code */
|
|
24
|
+
foldStartLines: Map<number, FoldRegion>;
|
|
25
|
+
__lineIdCounter: number;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Shiki transformer that adds code folding functionality for HTML-like tags.
|
|
31
|
+
*/
|
|
32
|
+
declare function transformerRenderHtmlFold(options?: TransformerHtmlFoldOptions): ShikiTransformer;
|
|
33
|
+
/** Detect foldable regions from raw token arrays */
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/listeners.d.ts
|
|
36
|
+
/**
|
|
37
|
+
* Attach a delegated click listener for fold toggles.
|
|
38
|
+
* Uses event delegation so it works with dynamically rendered content.
|
|
39
|
+
* Only needs to be called once.
|
|
40
|
+
*/
|
|
41
|
+
declare function attachFoldToggleListener(classPrefix?: string): void;
|
|
42
|
+
//#endregion
|
|
43
|
+
export { attachFoldToggleListener, transformerRenderHtmlFold };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
//#region src/html.ts
|
|
2
|
+
/** Self-closing/void HTML tags that don't need closing tags */
|
|
3
|
+
const VOID_TAGS = new Set([
|
|
4
|
+
"area",
|
|
5
|
+
"base",
|
|
6
|
+
"br",
|
|
7
|
+
"col",
|
|
8
|
+
"embed",
|
|
9
|
+
"hr",
|
|
10
|
+
"img",
|
|
11
|
+
"input",
|
|
12
|
+
"link",
|
|
13
|
+
"meta",
|
|
14
|
+
"param",
|
|
15
|
+
"source",
|
|
16
|
+
"track",
|
|
17
|
+
"wbr",
|
|
18
|
+
"path",
|
|
19
|
+
"circle",
|
|
20
|
+
"rect",
|
|
21
|
+
"line",
|
|
22
|
+
"polyline",
|
|
23
|
+
"polygon",
|
|
24
|
+
"ellipse"
|
|
25
|
+
]);
|
|
26
|
+
const OPEN_TAG_REGEX = /<([a-zA-Z][a-zA-Z0-9\-_:.]*)[^>]*(?<!\/)>/g;
|
|
27
|
+
const CLOSE_TAG_REGEX = /<\/([a-zA-Z][a-zA-Z0-9\-_:.]*)\s*>/g;
|
|
28
|
+
const SELF_CLOSING_REGEX = /<([a-zA-Z][a-zA-Z0-9\-_:.]*)[^>]*\/>/g;
|
|
29
|
+
/**
|
|
30
|
+
* Shiki transformer that adds code folding functionality for HTML-like tags.
|
|
31
|
+
*/
|
|
32
|
+
function transformerRenderHtmlFold(options = {}) {
|
|
33
|
+
const { classPrefix = "shiki-fold" } = options;
|
|
34
|
+
return {
|
|
35
|
+
name: "shiki-transformer-html-fold",
|
|
36
|
+
tokens(lines) {
|
|
37
|
+
const foldRegions = detectFoldRegions(lines);
|
|
38
|
+
const foldStartLines = new Map(foldRegions.map((r) => [r.startLine, r]));
|
|
39
|
+
this.meta.transformerHtmlFold = {
|
|
40
|
+
foldRegions,
|
|
41
|
+
foldStartLines,
|
|
42
|
+
__lineIdCounter: 0
|
|
43
|
+
};
|
|
44
|
+
return lines;
|
|
45
|
+
},
|
|
46
|
+
pre(element) {
|
|
47
|
+
element.children.unshift({
|
|
48
|
+
type: "element",
|
|
49
|
+
tagName: "style",
|
|
50
|
+
properties: {},
|
|
51
|
+
children: [{
|
|
52
|
+
type: "text",
|
|
53
|
+
value: getStyles(classPrefix)
|
|
54
|
+
}]
|
|
55
|
+
});
|
|
56
|
+
},
|
|
57
|
+
line(element, lineNumber) {
|
|
58
|
+
const { transformerHtmlFold } = this.meta;
|
|
59
|
+
const lineId = `${classPrefix}-${transformerHtmlFold.__lineIdCounter++}`;
|
|
60
|
+
element.properties["data-fold-line"] = lineId;
|
|
61
|
+
const region = transformerHtmlFold.foldStartLines.get(lineNumber);
|
|
62
|
+
if (region) {
|
|
63
|
+
const endLineId = `${classPrefix}-${region.endLine - 1}`;
|
|
64
|
+
element.properties["data-fold-end-id"] = endLineId;
|
|
65
|
+
element.properties["data-folded"] = "false";
|
|
66
|
+
element.properties.class = `${element.properties.class || ""} ${classPrefix}-foldable`.trim();
|
|
67
|
+
}
|
|
68
|
+
return element;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/** Detect foldable regions from raw token arrays */
|
|
73
|
+
function detectFoldRegions(lines) {
|
|
74
|
+
const regions = [];
|
|
75
|
+
const stack = [];
|
|
76
|
+
for (const [lineIndex, lineTokens] of lines.entries()) {
|
|
77
|
+
const text = lineTokens.map((t) => t.content).join("");
|
|
78
|
+
const lineNum = lineIndex + 1;
|
|
79
|
+
const selfClosingPositions = new Set();
|
|
80
|
+
for (const match of text.matchAll(SELF_CLOSING_REGEX)) selfClosingPositions.add(match.index);
|
|
81
|
+
for (const match of text.matchAll(OPEN_TAG_REGEX)) {
|
|
82
|
+
const [_, tagName] = match;
|
|
83
|
+
if (!tagName) continue;
|
|
84
|
+
if (selfClosingPositions.has(match.index)) continue;
|
|
85
|
+
const tagNameLower = tagName.toLowerCase();
|
|
86
|
+
if (!VOID_TAGS.has(tagNameLower)) stack.push({
|
|
87
|
+
tagName: tagNameLower,
|
|
88
|
+
line: lineNum
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
for (const match of text.matchAll(CLOSE_TAG_REGEX)) {
|
|
92
|
+
const [_, tagName] = match;
|
|
93
|
+
if (!tagName) continue;
|
|
94
|
+
const tagNameLower = tagName.toLowerCase();
|
|
95
|
+
for (let i = stack.length - 1; i >= 0; i--) if (stack[i]?.tagName === tagNameLower) {
|
|
96
|
+
const startLine = stack[i].line;
|
|
97
|
+
if (lineNum > startLine) regions.push({
|
|
98
|
+
startLine,
|
|
99
|
+
endLine: lineNum
|
|
100
|
+
});
|
|
101
|
+
stack.splice(i, 1);
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return regions;
|
|
107
|
+
}
|
|
108
|
+
/** CSS styles for fold functionality */
|
|
109
|
+
function getStyles(classPrefix) {
|
|
110
|
+
return `
|
|
111
|
+
.shiki code { display: grid; }
|
|
112
|
+
.${classPrefix}-foldable { cursor: pointer; }
|
|
113
|
+
.${classPrefix}-foldable:hover { background: rgba(255, 255, 255, 0.05); }
|
|
114
|
+
.${classPrefix}-hidden { display: none !important; }
|
|
115
|
+
.${classPrefix}-summary {
|
|
116
|
+
display: inline;
|
|
117
|
+
opacity: 0.5;
|
|
118
|
+
font-style: italic;
|
|
119
|
+
margin-left: 0.5em;
|
|
120
|
+
user-select: none;
|
|
121
|
+
pointer-events: none;
|
|
122
|
+
}`.trim();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
//#endregion
|
|
126
|
+
//#region src/listeners.ts
|
|
127
|
+
/**
|
|
128
|
+
* Attach a delegated click listener for fold toggles.
|
|
129
|
+
* Uses event delegation so it works with dynamically rendered content.
|
|
130
|
+
* Only needs to be called once.
|
|
131
|
+
*/
|
|
132
|
+
function attachFoldToggleListener(classPrefix = "shiki-fold") {
|
|
133
|
+
if (typeof document === "undefined") return;
|
|
134
|
+
const hiddenClass = `${classPrefix}-hidden`;
|
|
135
|
+
const summaryClass = `${classPrefix}-summary`;
|
|
136
|
+
document.addEventListener("click", (event) => {
|
|
137
|
+
const line = event.target.closest(`.${classPrefix}-foldable`);
|
|
138
|
+
if (!line) return;
|
|
139
|
+
const endId = line.dataset.foldEndId;
|
|
140
|
+
if (!endId) return;
|
|
141
|
+
const isFolded = line.dataset.folded === "true";
|
|
142
|
+
line.dataset.folded = String(!isFolded);
|
|
143
|
+
const linesToToggle = [];
|
|
144
|
+
let sibling = line.nextElementSibling;
|
|
145
|
+
while (sibling) {
|
|
146
|
+
const siblingId = sibling.dataset.foldLine;
|
|
147
|
+
linesToToggle.push(sibling);
|
|
148
|
+
if (siblingId === endId) break;
|
|
149
|
+
sibling = sibling.nextElementSibling;
|
|
150
|
+
}
|
|
151
|
+
const existingSummary = line.querySelector(`.${summaryClass}`);
|
|
152
|
+
if (isFolded) existingSummary?.remove();
|
|
153
|
+
else {
|
|
154
|
+
const count = linesToToggle.length;
|
|
155
|
+
const summary = document.createElement("span");
|
|
156
|
+
summary.className = summaryClass;
|
|
157
|
+
summary.textContent = `... ${count} line${count > 1 ? "s" : ""} hidden`;
|
|
158
|
+
line.append(summary);
|
|
159
|
+
}
|
|
160
|
+
for (const targetLine of linesToToggle) targetLine.classList.toggle(hiddenClass, !isFolded);
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
//#endregion
|
|
165
|
+
export { attachFoldToggleListener, transformerRenderHtmlFold };
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "shiki-transformer-fold",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Shiki transformer to bring code folding support",
|
|
5
|
+
"repository": "huang-julien/shiki-transformer-fold",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"type": "module",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./dist/index.mjs"
|
|
11
|
+
},
|
|
12
|
+
"types": "./dist/index.d.mts",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@types/hast": "^3.0.4",
|
|
18
|
+
"@types/node": "^24.3.0",
|
|
19
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
20
|
+
"automd": "^0.4.0",
|
|
21
|
+
"changelogen": "^0.6.2",
|
|
22
|
+
"eslint": "^9.33.0",
|
|
23
|
+
"eslint-config-unjs": "^0.5.0",
|
|
24
|
+
"hast": "^1.0.0",
|
|
25
|
+
"obuild": "^0.2.1",
|
|
26
|
+
"prettier": "^3.6.2",
|
|
27
|
+
"shiki": "^3.20.0",
|
|
28
|
+
"typescript": "^5.9.2",
|
|
29
|
+
"vitest": "^3.2.4"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "obuild",
|
|
33
|
+
"dev": "vite dev ./playground",
|
|
34
|
+
"lint": "eslint . && prettier -c .",
|
|
35
|
+
"lint:fix": "automd && eslint . --fix && prettier -w .",
|
|
36
|
+
"release": "pnpm test && changelogen --release && npm publish && git push --follow-tags",
|
|
37
|
+
"test": "pnpm lint && pnpm test:types && vitest run --coverage",
|
|
38
|
+
"test:types": "tsc --noEmit --skipLibCheck"
|
|
39
|
+
}
|
|
40
|
+
}
|