@limn-hl/rehype 0.0.0-stage → 0.5.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 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.
@@ -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,162 @@
1
+ import {
2
+ eachToken,
3
+ KIND_COUNT,
4
+ kindName,
5
+ TokenKind
6
+ } from "limn-hl";
7
+ export function rehypeLimn(options) {
8
+ const { registry, theme, detect } = options;
9
+ const prefix = options.classPrefix || "limn";
10
+ const classes = Boolean(options.classes);
11
+ const lineNumbers = Boolean(options.lineNumbers);
12
+ const spanProps = Array(KIND_COUNT);
13
+ for (let k = 1;k < KIND_COUNT; k++) {
14
+ const kind = k;
15
+ if (classes) {
16
+ spanProps[k] = { className: [`${prefix}-${kindName(kind)}`] };
17
+ continue;
18
+ }
19
+ const col = theme?.tokens[k] ?? "";
20
+ if (col || kind === TokenKind.Comment) {
21
+ const style = col && kind === TokenKind.Comment ? `color:${col};font-style:italic` : col ? `color:${col}` : "font-style:italic";
22
+ spanProps[k] = { style };
23
+ }
24
+ }
25
+ let blockStyle = "";
26
+ let gutterStyle = "";
27
+ if (!classes) {
28
+ const parts = lineNumbers ? ["display:flex", "white-space:pre", "overflow:auto"] : ["white-space:pre", "overflow:auto"];
29
+ if (theme?.scheme)
30
+ parts.push(`color-scheme:${theme.scheme}`);
31
+ if (theme?.bg)
32
+ parts.push(`background:${theme.bg}`);
33
+ if (theme?.fg)
34
+ parts.push(`color:${theme.fg}`);
35
+ blockStyle = parts.join(";");
36
+ if (lineNumbers) {
37
+ const numCol = theme?.numbers || theme?.tokens[TokenKind.Comment] || "";
38
+ gutterStyle = "padding-right:1em;text-align:right;user-select:none" + (numCol ? `;color:${numCol}` : "");
39
+ }
40
+ }
41
+ return function transform(tree) {
42
+ visit(tree);
43
+ };
44
+ function visit(node) {
45
+ if (!node.children)
46
+ return;
47
+ for (const child of node.children) {
48
+ if (child.type === "element" && "tagName" in child) {
49
+ if (child.tagName === "pre") {
50
+ const code = findSingleCode(child);
51
+ if (code) {
52
+ highlightPreCode(child, code);
53
+ continue;
54
+ }
55
+ }
56
+ visit(child);
57
+ } else if ("children" in child) {
58
+ visit(child);
59
+ }
60
+ }
61
+ }
62
+ function highlightPreCode(pre, code) {
63
+ const src = extractText(code).replace(/\r?\n$/, "");
64
+ let lang = extractLang(code.properties?.className);
65
+ if (!lang && detect) {
66
+ lang = detect(src, registry);
67
+ }
68
+ const out = [];
69
+ eachToken(src, registry.lookup(lang), registry, (tok) => {
70
+ const value = src.slice(tok.start, tok.end);
71
+ const props = spanProps[tok.kind];
72
+ out.push(props ? {
73
+ type: "element",
74
+ tagName: "span",
75
+ properties: { ...props },
76
+ children: [{ type: "text", value }]
77
+ } : { type: "text", value });
78
+ });
79
+ code.children = out;
80
+ const preProps = pre.properties ??= {};
81
+ const cls = new Set(toClassList(preProps.className));
82
+ cls.add(prefix);
83
+ if (lang) {
84
+ cls.add(`${prefix}-lang-${lang}`);
85
+ preProps.dataLang = lang;
86
+ }
87
+ if (lineNumbers) {
88
+ cls.add(`${prefix}-numbered`);
89
+ }
90
+ preProps.className = [...cls];
91
+ if (blockStyle) {
92
+ const existing = preProps.style?.trim();
93
+ preProps.style = existing ? `${existing.replace(/;$/, "")};${blockStyle}` : blockStyle;
94
+ }
95
+ if (lineNumbers) {
96
+ const lineCount = src.split(`
97
+ `).length;
98
+ const nums = Array.from({ length: lineCount }, (_, i) => i + 1).join(`
99
+ `);
100
+ const gutterProps = {
101
+ className: [`${prefix}-numbers`],
102
+ ariaHidden: "true"
103
+ };
104
+ if (gutterStyle)
105
+ gutterProps.style = gutterStyle;
106
+ pre.children = [
107
+ {
108
+ type: "element",
109
+ tagName: "span",
110
+ properties: gutterProps,
111
+ children: [{ type: "text", value: nums }]
112
+ },
113
+ code
114
+ ];
115
+ }
116
+ }
117
+ }
118
+ export default rehypeLimn;
119
+ function findSingleCode(pre) {
120
+ let code = null;
121
+ for (const child of pre.children) {
122
+ if (child.type === "text" && "value" in child) {
123
+ if (child.value.trim() !== "")
124
+ return null;
125
+ continue;
126
+ }
127
+ if (child.type === "element" && "tagName" in child && child.tagName === "code") {
128
+ if (code)
129
+ return null;
130
+ code = child;
131
+ continue;
132
+ }
133
+ return null;
134
+ }
135
+ return code;
136
+ }
137
+ function extractText(node) {
138
+ if (node.type === "text" && "value" in node)
139
+ return node.value;
140
+ if (!("children" in node) || !node.children)
141
+ return "";
142
+ let out = "";
143
+ for (const child of node.children)
144
+ out += extractText(child);
145
+ return out;
146
+ }
147
+ function extractLang(className) {
148
+ for (const cls of toClassList(className)) {
149
+ if (cls.startsWith("language-"))
150
+ return cls.slice(9);
151
+ if (cls.startsWith("lang-"))
152
+ return cls.slice(5);
153
+ }
154
+ return "";
155
+ }
156
+ function toClassList(value) {
157
+ if (Array.isArray(value))
158
+ return value.map(String).filter(Boolean);
159
+ if (typeof value === "string")
160
+ return value.split(/\s+/).filter(Boolean);
161
+ return [];
162
+ }
package/package.json CHANGED
@@ -1,6 +1,38 @@
1
1
  {
2
2
  "name": "@limn-hl/rehype",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.5.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
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/ekesti/limn.git",
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.5.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"
37
+ }
38
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.