@limn-hl/rehype 0.0.0 → 0.6.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 +38 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +172 -0
- package/package.json +29 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ray <https://github.com/so1ve>
|
|
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,38 @@
|
|
|
1
|
+
# @limn-hl/rehype
|
|
2
|
+
|
|
3
|
+
Rehype plugin for [`limn-hl`](https://github.com/ekesti/limn). Builds HAST `<span>` nodes directly from `eachToken` with zero HTML re-parsing.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install limn-hl @limn-hl/rehype
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import rehypeLimn from "@limn-hl/rehype";
|
|
15
|
+
import { detectIn } from "limn-hl/detect";
|
|
16
|
+
import { all } from "limn-hl/lang";
|
|
17
|
+
import { tokyoNight } from "limn-hl/theme";
|
|
18
|
+
import rehypeStringify from "rehype-stringify";
|
|
19
|
+
import remarkParse from "remark-parse";
|
|
20
|
+
import remarkRehype from "remark-rehype";
|
|
21
|
+
import { unified } from "unified";
|
|
22
|
+
|
|
23
|
+
const html = String(
|
|
24
|
+
unified()
|
|
25
|
+
.use(remarkParse)
|
|
26
|
+
.use(remarkRehype)
|
|
27
|
+
.use(rehypeLimn, {
|
|
28
|
+
registry: all(),
|
|
29
|
+
theme: tokyoNight,
|
|
30
|
+
lineNumbers: true,
|
|
31
|
+
detect: detectIn,
|
|
32
|
+
})
|
|
33
|
+
.use(rehypeStringify)
|
|
34
|
+
.processSync(markdown),
|
|
35
|
+
);
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Pass a custom `new Registry([...])` instead of `all()` and omit `detect` to tree-shake unused languages and the detector. See [`docs/integrations.md`](https://github.com/ekesti/limn/blob/main/docs/integrations.md) for full options and CSS-class styling.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type Registry } from "limn-hl";
|
|
2
|
+
import type { HtmlOptions } from "limn-hl/render";
|
|
3
|
+
/** Language detector compatible with detect and detectIn from "limn-hl/detect". */
|
|
4
|
+
export type DetectFn = (src: string, registry: Registry) => string;
|
|
5
|
+
/** Options configuring the @limn-hl/rehype plugin. */
|
|
6
|
+
export interface RehypeLimnOptions extends Omit<HtmlOptions, "wrapper"> {
|
|
7
|
+
/** Grammar registry (e.g. all() from "limn-hl/lang" or a custom Registry). */
|
|
8
|
+
registry: Registry;
|
|
9
|
+
/** Optional detector for unlabeled code blocks (e.g. detectIn from "limn-hl/detect"). */
|
|
10
|
+
detect?: DetectFn;
|
|
11
|
+
}
|
|
12
|
+
export interface HastRoot {
|
|
13
|
+
type: "root";
|
|
14
|
+
children: HastNode[];
|
|
15
|
+
}
|
|
16
|
+
export interface HastElement {
|
|
17
|
+
type: "element";
|
|
18
|
+
tagName: string;
|
|
19
|
+
properties?: HastProperties;
|
|
20
|
+
children: HastNode[];
|
|
21
|
+
}
|
|
22
|
+
export type HastNode = HastElement | {
|
|
23
|
+
type: "text";
|
|
24
|
+
value: string;
|
|
25
|
+
} | {
|
|
26
|
+
type: string;
|
|
27
|
+
children?: HastNode[];
|
|
28
|
+
};
|
|
29
|
+
interface HastProperties {
|
|
30
|
+
[key: string]: unknown;
|
|
31
|
+
className?: string | number | boolean | (string | number)[] | null;
|
|
32
|
+
style?: string;
|
|
33
|
+
dataLang?: string;
|
|
34
|
+
ariaHidden?: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Rehype plugin that highlights <pre><code> blocks directly in HAST
|
|
38
|
+
* without HTML string serialization or re-parsing.
|
|
39
|
+
*/
|
|
40
|
+
export declare function rehypeLimn(options: RehypeLimnOptions): (tree: HastRoot) => void;
|
|
41
|
+
export default rehypeLimn;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import {
|
|
2
|
+
eachToken,
|
|
3
|
+
KIND_COUNT,
|
|
4
|
+
kindName,
|
|
5
|
+
TokenKind
|
|
6
|
+
} from "limn-hl";
|
|
7
|
+
import { FontStyle } from "limn-hl/theme";
|
|
8
|
+
export function rehypeLimn(options) {
|
|
9
|
+
const { registry, theme, detect } = options;
|
|
10
|
+
const prefix = options.classPrefix || "limn";
|
|
11
|
+
const classes = Boolean(options.classes);
|
|
12
|
+
const lineNumbers = Boolean(options.lineNumbers);
|
|
13
|
+
const spanProps = Array(KIND_COUNT);
|
|
14
|
+
for (let k = 1;k < KIND_COUNT; k++) {
|
|
15
|
+
const kind = k;
|
|
16
|
+
if (classes) {
|
|
17
|
+
spanProps[k] = { className: [`${prefix}-${kindName(kind)}`] };
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
const col = theme?.tokens[k] ?? "";
|
|
21
|
+
const styleBits = theme?.styles?.[k] ?? 0;
|
|
22
|
+
if (col || styleBits) {
|
|
23
|
+
const parts = [];
|
|
24
|
+
if (col)
|
|
25
|
+
parts.push(`color:${col}`);
|
|
26
|
+
if (styleBits & FontStyle.Bold)
|
|
27
|
+
parts.push("font-weight:bold");
|
|
28
|
+
if (styleBits & FontStyle.Italic)
|
|
29
|
+
parts.push("font-style:italic");
|
|
30
|
+
if (styleBits & FontStyle.Underline)
|
|
31
|
+
parts.push("text-decoration:underline");
|
|
32
|
+
spanProps[k] = { style: parts.join(";") };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
let blockStyle = "";
|
|
36
|
+
let gutterStyle = "";
|
|
37
|
+
if (!classes) {
|
|
38
|
+
const parts = lineNumbers ? ["display:flex", "white-space:pre", "overflow:auto"] : ["white-space:pre", "overflow:auto"];
|
|
39
|
+
if (theme?.scheme)
|
|
40
|
+
parts.push(`color-scheme:${theme.scheme}`);
|
|
41
|
+
if (theme?.bg)
|
|
42
|
+
parts.push(`background:${theme.bg}`);
|
|
43
|
+
if (theme?.fg)
|
|
44
|
+
parts.push(`color:${theme.fg}`);
|
|
45
|
+
blockStyle = parts.join(";");
|
|
46
|
+
if (lineNumbers) {
|
|
47
|
+
const numCol = theme?.numbers || theme?.tokens[TokenKind.Comment] || "";
|
|
48
|
+
gutterStyle = "padding-right:1em;text-align:right;user-select:none" + (numCol ? `;color:${numCol}` : "");
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return function transform(tree) {
|
|
52
|
+
visit(tree);
|
|
53
|
+
};
|
|
54
|
+
function visit(node) {
|
|
55
|
+
if (!node.children)
|
|
56
|
+
return;
|
|
57
|
+
for (const child of node.children) {
|
|
58
|
+
if (child.type === "element" && "tagName" in child) {
|
|
59
|
+
if (child.tagName === "pre") {
|
|
60
|
+
const code = findSingleCode(child);
|
|
61
|
+
if (code) {
|
|
62
|
+
highlightPreCode(child, code);
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
visit(child);
|
|
67
|
+
} else if ("children" in child) {
|
|
68
|
+
visit(child);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function highlightPreCode(pre, code) {
|
|
73
|
+
const src = extractText(code).replace(/\r?\n$/, "");
|
|
74
|
+
let lang = extractLang(code.properties?.className);
|
|
75
|
+
if (!lang && detect) {
|
|
76
|
+
lang = detect(src, registry);
|
|
77
|
+
}
|
|
78
|
+
const out = [];
|
|
79
|
+
eachToken(src, registry.lookup(lang), registry, (tok) => {
|
|
80
|
+
const value = src.slice(tok.start, tok.end);
|
|
81
|
+
const props = spanProps[tok.kind];
|
|
82
|
+
out.push(props ? {
|
|
83
|
+
type: "element",
|
|
84
|
+
tagName: "span",
|
|
85
|
+
properties: { ...props },
|
|
86
|
+
children: [{ type: "text", value }]
|
|
87
|
+
} : { type: "text", value });
|
|
88
|
+
});
|
|
89
|
+
code.children = out;
|
|
90
|
+
const preProps = pre.properties ??= {};
|
|
91
|
+
const cls = new Set(toClassList(preProps.className));
|
|
92
|
+
cls.add(prefix);
|
|
93
|
+
if (lang) {
|
|
94
|
+
cls.add(`${prefix}-lang-${lang}`);
|
|
95
|
+
preProps.dataLang = lang;
|
|
96
|
+
}
|
|
97
|
+
if (lineNumbers) {
|
|
98
|
+
cls.add(`${prefix}-numbered`);
|
|
99
|
+
}
|
|
100
|
+
preProps.className = [...cls];
|
|
101
|
+
if (blockStyle) {
|
|
102
|
+
const existing = preProps.style?.trim();
|
|
103
|
+
preProps.style = existing ? `${existing.replace(/;$/, "")};${blockStyle}` : blockStyle;
|
|
104
|
+
}
|
|
105
|
+
if (lineNumbers) {
|
|
106
|
+
const lineCount = src.split(`
|
|
107
|
+
`).length;
|
|
108
|
+
const nums = Array.from({ length: lineCount }, (_, i) => i + 1).join(`
|
|
109
|
+
`);
|
|
110
|
+
const gutterProps = {
|
|
111
|
+
className: [`${prefix}-numbers`],
|
|
112
|
+
ariaHidden: "true"
|
|
113
|
+
};
|
|
114
|
+
if (gutterStyle)
|
|
115
|
+
gutterProps.style = gutterStyle;
|
|
116
|
+
pre.children = [
|
|
117
|
+
{
|
|
118
|
+
type: "element",
|
|
119
|
+
tagName: "span",
|
|
120
|
+
properties: gutterProps,
|
|
121
|
+
children: [{ type: "text", value: nums }]
|
|
122
|
+
},
|
|
123
|
+
code
|
|
124
|
+
];
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
export default rehypeLimn;
|
|
129
|
+
function findSingleCode(pre) {
|
|
130
|
+
let code = null;
|
|
131
|
+
for (const child of pre.children) {
|
|
132
|
+
if (child.type === "text" && "value" in child) {
|
|
133
|
+
if (child.value.trim() !== "")
|
|
134
|
+
return null;
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (child.type === "element" && "tagName" in child && child.tagName === "code") {
|
|
138
|
+
if (code)
|
|
139
|
+
return null;
|
|
140
|
+
code = child;
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
return code;
|
|
146
|
+
}
|
|
147
|
+
function extractText(node) {
|
|
148
|
+
if (node.type === "text" && "value" in node)
|
|
149
|
+
return node.value;
|
|
150
|
+
if (!("children" in node) || !node.children)
|
|
151
|
+
return "";
|
|
152
|
+
let out = "";
|
|
153
|
+
for (const child of node.children)
|
|
154
|
+
out += extractText(child);
|
|
155
|
+
return out;
|
|
156
|
+
}
|
|
157
|
+
function extractLang(className) {
|
|
158
|
+
for (const cls of toClassList(className)) {
|
|
159
|
+
if (cls.startsWith("language-"))
|
|
160
|
+
return cls.slice(9);
|
|
161
|
+
if (cls.startsWith("lang-"))
|
|
162
|
+
return cls.slice(5);
|
|
163
|
+
}
|
|
164
|
+
return "";
|
|
165
|
+
}
|
|
166
|
+
function toClassList(value) {
|
|
167
|
+
if (Array.isArray(value))
|
|
168
|
+
return value.map(String).filter(Boolean);
|
|
169
|
+
if (typeof value === "string")
|
|
170
|
+
return value.split(/\s+/).filter(Boolean);
|
|
171
|
+
return [];
|
|
172
|
+
}
|
package/package.json
CHANGED
|
@@ -1,15 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@limn-hl/rehype",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Rehype plugin for synchronous syntax highlighting with limn",
|
|
5
|
-
"keywords": ["limn", "limn-hl", "rehype", "rehype-limn", "syntax-highlighting"],
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "Rehype plugin for synchronous syntax highlighting with limn (zero HTML re-parsing)",
|
|
5
|
+
"keywords": ["limn", "limn-hl", "rehype", "rehype-limn", "syntax-highlighting", "unified", "mdx"],
|
|
6
6
|
"license": "MIT",
|
|
7
|
-
"publishConfig": {
|
|
8
|
-
"access": "public"
|
|
9
|
-
},
|
|
10
7
|
"repository": {
|
|
11
8
|
"type": "git",
|
|
12
9
|
"url": "git+https://github.com/ekesti/limn.git",
|
|
13
10
|
"directory": "integrations/js/rehype"
|
|
11
|
+
},
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"main": "./dist/index.js",
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"limn-hl": "^0.6.0"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build:js": "bun build src/index.ts --root src --outdir dist --format esm --no-bundle --target browser",
|
|
33
|
+
"build:types": "tsc -p tsconfig.build.json",
|
|
34
|
+
"build": "rm -rf dist && bun run build:js && bun run build:types",
|
|
35
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
36
|
+
"test": "bun test test"
|
|
14
37
|
}
|
|
15
38
|
}
|