@unifast/node 0.0.4
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/dist/index.cjs +342 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.mjs +78 -0
- package/native/unifast.darwin-arm64.node +0 -0
- package/native/unifast.darwin-x64.node +0 -0
- package/native/unifast.linux-arm64-gnu.node +0 -0
- package/native/unifast.linux-x64-gnu.node +0 -0
- package/native/unifast.linux-x64-musl.node +0 -0
- package/native/unifast.win32-x64.node +0 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 kenzwada
|
|
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/dist/index.cjs
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
23
|
+
//#endregion
|
|
24
|
+
let deepmerge = require("deepmerge");
|
|
25
|
+
deepmerge = __toESM(deepmerge);
|
|
26
|
+
let node_module = require("node:module");
|
|
27
|
+
let _unifast_core = require("@unifast/core");
|
|
28
|
+
//#region src/native.ts
|
|
29
|
+
let nativeBinding = null;
|
|
30
|
+
function loadNativeBinding() {
|
|
31
|
+
if (nativeBinding) return nativeBinding;
|
|
32
|
+
try {
|
|
33
|
+
nativeBinding = (0, node_module.createRequire)(require("url").pathToFileURL(__filename).href)("../native/unifast.node");
|
|
34
|
+
} catch {
|
|
35
|
+
throw new Error("Failed to load unifast native binding. Make sure the native addon is built. Run `cargo build -p unifast-bindings-node --release` first.");
|
|
36
|
+
}
|
|
37
|
+
return nativeBinding;
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/index.ts
|
|
41
|
+
function applyDataLineAttributes(node) {
|
|
42
|
+
if (node.type === "element") if (node.tagName === "pre") {
|
|
43
|
+
const code = node.children.find((c) => c.type === "element" && c.tagName === "code");
|
|
44
|
+
if (code) {
|
|
45
|
+
let lineNum = 1;
|
|
46
|
+
for (const child of code.children) if (child.type === "element" && child.tagName === "span") {
|
|
47
|
+
const cls = child.properties.className ?? child.properties.class;
|
|
48
|
+
if (Array.isArray(cls) ? cls.includes("line") : typeof cls === "string" && cls.split(" ").includes("line")) child.properties["data-line"] = String(lineNum++);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
} else for (const child of node.children) applyDataLineAttributes(child);
|
|
52
|
+
else if (node.type === "root") for (const child of node.children) applyDataLineAttributes(child);
|
|
53
|
+
}
|
|
54
|
+
function compile(input, options) {
|
|
55
|
+
const native = loadNativeBinding();
|
|
56
|
+
const plugins = options?.plugins ?? [];
|
|
57
|
+
const hastTransforms = plugins.filter((p) => Boolean(p.hastTransform)).map((p) => p.hastTransform);
|
|
58
|
+
const mdxJsTransforms = plugins.filter((p) => Boolean(p.mdxJsTransform)).map((p) => p.mdxJsTransform);
|
|
59
|
+
let mergedOpts = { ...options };
|
|
60
|
+
delete mergedOpts.plugins;
|
|
61
|
+
for (const plugin of plugins) if (plugin.options) mergedOpts = (0, deepmerge.default)(mergedOpts, plugin.options);
|
|
62
|
+
const hasHastTransforms = hastTransforms.length > 0;
|
|
63
|
+
const userRequestedOutputKind = options?.outputKind;
|
|
64
|
+
if (hasHastTransforms && userRequestedOutputKind !== "hast" && userRequestedOutputKind !== "mdxJs" && userRequestedOutputKind !== "mdast") mergedOpts.outputKind = "hast";
|
|
65
|
+
const rawResult = native.compile(input, mergedOpts);
|
|
66
|
+
let { output } = rawResult;
|
|
67
|
+
if (hasHastTransforms && userRequestedOutputKind !== "mdxJs" && userRequestedOutputKind !== "mdast") {
|
|
68
|
+
let hast;
|
|
69
|
+
try {
|
|
70
|
+
hast = JSON.parse(output);
|
|
71
|
+
} catch {
|
|
72
|
+
throw new Error("Failed to parse HAST output from native binding");
|
|
73
|
+
}
|
|
74
|
+
for (const transform of hastTransforms) hast = transform(hast);
|
|
75
|
+
if (options?.lineNumbers?.enabled) for (const child of hast.children) applyDataLineAttributes(child);
|
|
76
|
+
output = userRequestedOutputKind === "hast" ? JSON.stringify(hast) : native.stringifyHast(JSON.stringify(hast));
|
|
77
|
+
}
|
|
78
|
+
if (mdxJsTransforms.length > 0 && (userRequestedOutputKind === "mdxJs" || mergedOpts.outputKind === "mdxJs")) for (const transform of mdxJsTransforms) output = transform(output);
|
|
79
|
+
let frontmatter;
|
|
80
|
+
try {
|
|
81
|
+
frontmatter = JSON.parse(rawResult.frontmatter);
|
|
82
|
+
} catch {
|
|
83
|
+
frontmatter = {};
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
output,
|
|
87
|
+
sourcemap: rawResult.sourcemap ?? void 0,
|
|
88
|
+
frontmatter,
|
|
89
|
+
diagnostics: rawResult.diagnostics.map((d) => ({
|
|
90
|
+
level: d.level,
|
|
91
|
+
message: d.message,
|
|
92
|
+
start: d.start,
|
|
93
|
+
end: d.end
|
|
94
|
+
})),
|
|
95
|
+
stats: rawResult.stats,
|
|
96
|
+
toc: rawResult.toc ?? [],
|
|
97
|
+
readingTime: rawResult.readingTime ?? void 0,
|
|
98
|
+
excerpt: rawResult.excerpt ?? void 0
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
//#endregion
|
|
102
|
+
Object.defineProperty(exports, "CompileError", {
|
|
103
|
+
enumerable: true,
|
|
104
|
+
get: function() {
|
|
105
|
+
return _unifast_core.CompileError;
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
Object.defineProperty(exports, "ParseError", {
|
|
109
|
+
enumerable: true,
|
|
110
|
+
get: function() {
|
|
111
|
+
return _unifast_core.ParseError;
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
Object.defineProperty(exports, "UnifastError", {
|
|
115
|
+
enumerable: true,
|
|
116
|
+
get: function() {
|
|
117
|
+
return _unifast_core.UnifastError;
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
Object.defineProperty(exports, "abbr", {
|
|
121
|
+
enumerable: true,
|
|
122
|
+
get: function() {
|
|
123
|
+
return _unifast_core.abbr;
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
Object.defineProperty(exports, "accessibleEmoji", {
|
|
127
|
+
enumerable: true,
|
|
128
|
+
get: function() {
|
|
129
|
+
return _unifast_core.accessibleEmoji;
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
Object.defineProperty(exports, "addClasses", {
|
|
133
|
+
enumerable: true,
|
|
134
|
+
get: function() {
|
|
135
|
+
return _unifast_core.addClasses;
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
Object.defineProperty(exports, "autolinkHeadings", {
|
|
139
|
+
enumerable: true,
|
|
140
|
+
get: function() {
|
|
141
|
+
return _unifast_core.autolinkHeadings;
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
Object.defineProperty(exports, "breaks", {
|
|
145
|
+
enumerable: true,
|
|
146
|
+
get: function() {
|
|
147
|
+
return _unifast_core.breaks;
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
Object.defineProperty(exports, "cjk", {
|
|
151
|
+
enumerable: true,
|
|
152
|
+
get: function() {
|
|
153
|
+
return _unifast_core.cjk;
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
Object.defineProperty(exports, "codeImport", {
|
|
157
|
+
enumerable: true,
|
|
158
|
+
get: function() {
|
|
159
|
+
return _unifast_core.codeImport;
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
Object.defineProperty(exports, "codeMeta", {
|
|
163
|
+
enumerable: true,
|
|
164
|
+
get: function() {
|
|
165
|
+
return _unifast_core.codeMeta;
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
Object.defineProperty(exports, "commentRemoval", {
|
|
169
|
+
enumerable: true,
|
|
170
|
+
get: function() {
|
|
171
|
+
return _unifast_core.commentRemoval;
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
exports.compile = compile;
|
|
175
|
+
Object.defineProperty(exports, "customHeadingId", {
|
|
176
|
+
enumerable: true,
|
|
177
|
+
get: function() {
|
|
178
|
+
return _unifast_core.customHeadingId;
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
Object.defineProperty(exports, "definitionList", {
|
|
182
|
+
enumerable: true,
|
|
183
|
+
get: function() {
|
|
184
|
+
return _unifast_core.definitionList;
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
Object.defineProperty(exports, "directive", {
|
|
188
|
+
enumerable: true,
|
|
189
|
+
get: function() {
|
|
190
|
+
return _unifast_core.directive;
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
Object.defineProperty(exports, "emoji", {
|
|
194
|
+
enumerable: true,
|
|
195
|
+
get: function() {
|
|
196
|
+
return _unifast_core.emoji;
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
Object.defineProperty(exports, "escapeHtml", {
|
|
200
|
+
enumerable: true,
|
|
201
|
+
get: function() {
|
|
202
|
+
return _unifast_core.escapeHtml;
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
Object.defineProperty(exports, "excerpt", {
|
|
206
|
+
enumerable: true,
|
|
207
|
+
get: function() {
|
|
208
|
+
return _unifast_core.excerpt;
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
Object.defineProperty(exports, "externalLinks", {
|
|
212
|
+
enumerable: true,
|
|
213
|
+
get: function() {
|
|
214
|
+
return _unifast_core.externalLinks;
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
Object.defineProperty(exports, "extractLang", {
|
|
218
|
+
enumerable: true,
|
|
219
|
+
get: function() {
|
|
220
|
+
return _unifast_core.extractLang;
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
Object.defineProperty(exports, "extractText", {
|
|
224
|
+
enumerable: true,
|
|
225
|
+
get: function() {
|
|
226
|
+
return _unifast_core.extractText;
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
Object.defineProperty(exports, "figure", {
|
|
230
|
+
enumerable: true,
|
|
231
|
+
get: function() {
|
|
232
|
+
return _unifast_core.figure;
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
Object.defineProperty(exports, "findCodeChild", {
|
|
236
|
+
enumerable: true,
|
|
237
|
+
get: function() {
|
|
238
|
+
return _unifast_core.findCodeChild;
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
Object.defineProperty(exports, "frontmatter", {
|
|
242
|
+
enumerable: true,
|
|
243
|
+
get: function() {
|
|
244
|
+
return _unifast_core.frontmatter;
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
Object.defineProperty(exports, "gfm", {
|
|
248
|
+
enumerable: true,
|
|
249
|
+
get: function() {
|
|
250
|
+
return _unifast_core.gfm;
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
Object.defineProperty(exports, "githubAlert", {
|
|
254
|
+
enumerable: true,
|
|
255
|
+
get: function() {
|
|
256
|
+
return _unifast_core.githubAlert;
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
Object.defineProperty(exports, "hastToHtml", {
|
|
260
|
+
enumerable: true,
|
|
261
|
+
get: function() {
|
|
262
|
+
return _unifast_core.hastToHtml;
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
Object.defineProperty(exports, "imgLazyLoading", {
|
|
266
|
+
enumerable: true,
|
|
267
|
+
get: function() {
|
|
268
|
+
return _unifast_core.imgLazyLoading;
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
Object.defineProperty(exports, "math", {
|
|
272
|
+
enumerable: true,
|
|
273
|
+
get: function() {
|
|
274
|
+
return _unifast_core.math;
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
Object.defineProperty(exports, "minify", {
|
|
278
|
+
enumerable: true,
|
|
279
|
+
get: function() {
|
|
280
|
+
return _unifast_core.minify;
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
Object.defineProperty(exports, "readingTime", {
|
|
284
|
+
enumerable: true,
|
|
285
|
+
get: function() {
|
|
286
|
+
return _unifast_core.readingTime;
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
Object.defineProperty(exports, "rubyAnnotation", {
|
|
290
|
+
enumerable: true,
|
|
291
|
+
get: function() {
|
|
292
|
+
return _unifast_core.rubyAnnotation;
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
Object.defineProperty(exports, "sanitize", {
|
|
296
|
+
enumerable: true,
|
|
297
|
+
get: function() {
|
|
298
|
+
return _unifast_core.sanitize;
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
Object.defineProperty(exports, "sectionize", {
|
|
302
|
+
enumerable: true,
|
|
303
|
+
get: function() {
|
|
304
|
+
return _unifast_core.sectionize;
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
Object.defineProperty(exports, "smartypants", {
|
|
308
|
+
enumerable: true,
|
|
309
|
+
get: function() {
|
|
310
|
+
return _unifast_core.smartypants;
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
Object.defineProperty(exports, "syntect", {
|
|
314
|
+
enumerable: true,
|
|
315
|
+
get: function() {
|
|
316
|
+
return _unifast_core.syntect;
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
Object.defineProperty(exports, "toc", {
|
|
320
|
+
enumerable: true,
|
|
321
|
+
get: function() {
|
|
322
|
+
return _unifast_core.toc;
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
Object.defineProperty(exports, "treeSitter", {
|
|
326
|
+
enumerable: true,
|
|
327
|
+
get: function() {
|
|
328
|
+
return _unifast_core.treeSitter;
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
Object.defineProperty(exports, "visitHast", {
|
|
332
|
+
enumerable: true,
|
|
333
|
+
get: function() {
|
|
334
|
+
return _unifast_core.visitHast;
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
Object.defineProperty(exports, "wikiLink", {
|
|
338
|
+
enumerable: true,
|
|
339
|
+
get: function() {
|
|
340
|
+
return _unifast_core.wikiLink;
|
|
341
|
+
}
|
|
342
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { AutolinkHeadingsPluginOptions, CodeImportPluginOptions, CompileError, CompileOptions, CompileOptions as CompileOptions$1, CompileResult, CompileResult as CompileResult$1, ExcerptPluginOptions, ExternalLinksPluginOptions, FrontmatterPluginOptions, GfmPluginOptions, GithubAlertIconDef, GithubAlertPluginOptions, HastElement, HastNode, HastRoot, HastText, ImgLazyLoadingPluginOptions, ParseError, ReadingTimePluginOptions, SanitizePluginOptions, SmartypantsPluginOptions, SyntectPluginOptions, TocEntry, TocPluginOptions, TreeSitterPluginOptions, UnifastError, UnifastPlugin, WikiLinkPluginOptions, abbr, accessibleEmoji, addClasses, autolinkHeadings, breaks, cjk, codeImport, codeMeta, commentRemoval, customHeadingId, definitionList, directive, emoji, escapeHtml, excerpt, externalLinks, extractLang, extractText, figure, findCodeChild, frontmatter, gfm, githubAlert, hastToHtml, imgLazyLoading, math, minify, readingTime, rubyAnnotation, sanitize, sectionize, smartypants, syntect, toc, treeSitter, visitHast, wikiLink } from "@unifast/core";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
declare function compile(input: string, options?: CompileOptions$1): CompileResult$1;
|
|
5
|
+
//#endregion
|
|
6
|
+
export { type AutolinkHeadingsPluginOptions, type CodeImportPluginOptions, CompileError, type CompileOptions, type CompileResult, type ExcerptPluginOptions, type ExternalLinksPluginOptions, type FrontmatterPluginOptions, type GfmPluginOptions, type GithubAlertIconDef, type GithubAlertPluginOptions, type HastElement, type HastNode, type HastRoot, type HastText, type ImgLazyLoadingPluginOptions, ParseError, type ReadingTimePluginOptions, type SanitizePluginOptions, type SmartypantsPluginOptions, type SyntectPluginOptions, type TocEntry, type TocPluginOptions, type TreeSitterPluginOptions, UnifastError, type UnifastPlugin, type WikiLinkPluginOptions, abbr, accessibleEmoji, addClasses, autolinkHeadings, breaks, cjk, codeImport, codeMeta, commentRemoval, compile, customHeadingId, definitionList, directive, emoji, escapeHtml, excerpt, externalLinks, extractLang, extractText, figure, findCodeChild, frontmatter, gfm, githubAlert, hastToHtml, imgLazyLoading, math, minify, readingTime, rubyAnnotation, sanitize, sectionize, smartypants, syntect, toc, treeSitter, visitHast, wikiLink };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { AutolinkHeadingsPluginOptions, CodeImportPluginOptions, CompileError, CompileOptions, CompileOptions as CompileOptions$1, CompileResult, CompileResult as CompileResult$1, ExcerptPluginOptions, ExternalLinksPluginOptions, FrontmatterPluginOptions, GfmPluginOptions, GithubAlertIconDef, GithubAlertPluginOptions, HastElement, HastNode, HastRoot, HastText, ImgLazyLoadingPluginOptions, ParseError, ReadingTimePluginOptions, SanitizePluginOptions, SmartypantsPluginOptions, SyntectPluginOptions, TocEntry, TocPluginOptions, TreeSitterPluginOptions, UnifastError, UnifastPlugin, WikiLinkPluginOptions, abbr, accessibleEmoji, addClasses, autolinkHeadings, breaks, cjk, codeImport, codeMeta, commentRemoval, customHeadingId, definitionList, directive, emoji, escapeHtml, excerpt, externalLinks, extractLang, extractText, figure, findCodeChild, frontmatter, gfm, githubAlert, hastToHtml, imgLazyLoading, math, minify, readingTime, rubyAnnotation, sanitize, sectionize, smartypants, syntect, toc, treeSitter, visitHast, wikiLink } from "@unifast/core";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
declare function compile(input: string, options?: CompileOptions$1): CompileResult$1;
|
|
5
|
+
//#endregion
|
|
6
|
+
export { type AutolinkHeadingsPluginOptions, type CodeImportPluginOptions, CompileError, type CompileOptions, type CompileResult, type ExcerptPluginOptions, type ExternalLinksPluginOptions, type FrontmatterPluginOptions, type GfmPluginOptions, type GithubAlertIconDef, type GithubAlertPluginOptions, type HastElement, type HastNode, type HastRoot, type HastText, type ImgLazyLoadingPluginOptions, ParseError, type ReadingTimePluginOptions, type SanitizePluginOptions, type SmartypantsPluginOptions, type SyntectPluginOptions, type TocEntry, type TocPluginOptions, type TreeSitterPluginOptions, UnifastError, type UnifastPlugin, type WikiLinkPluginOptions, abbr, accessibleEmoji, addClasses, autolinkHeadings, breaks, cjk, codeImport, codeMeta, commentRemoval, compile, customHeadingId, definitionList, directive, emoji, escapeHtml, excerpt, externalLinks, extractLang, extractText, figure, findCodeChild, frontmatter, gfm, githubAlert, hastToHtml, imgLazyLoading, math, minify, readingTime, rubyAnnotation, sanitize, sectionize, smartypants, syntect, toc, treeSitter, visitHast, wikiLink };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import deepmerge from "deepmerge";
|
|
3
|
+
import { CompileError, ParseError, UnifastError, abbr, accessibleEmoji, addClasses, autolinkHeadings, breaks, cjk, codeImport, codeMeta, commentRemoval, customHeadingId, definitionList, directive, emoji, escapeHtml, excerpt, externalLinks, extractLang, extractText, figure, findCodeChild, frontmatter, gfm, githubAlert, hastToHtml, imgLazyLoading, math, minify, readingTime, rubyAnnotation, sanitize, sectionize, smartypants, syntect, toc, treeSitter, visitHast, wikiLink } from "@unifast/core";
|
|
4
|
+
//#region src/native.ts
|
|
5
|
+
let nativeBinding = null;
|
|
6
|
+
function loadNativeBinding() {
|
|
7
|
+
if (nativeBinding) return nativeBinding;
|
|
8
|
+
try {
|
|
9
|
+
nativeBinding = createRequire(import.meta.url)("../native/unifast.node");
|
|
10
|
+
} catch {
|
|
11
|
+
throw new Error("Failed to load unifast native binding. Make sure the native addon is built. Run `cargo build -p unifast-bindings-node --release` first.");
|
|
12
|
+
}
|
|
13
|
+
return nativeBinding;
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/index.ts
|
|
17
|
+
function applyDataLineAttributes(node) {
|
|
18
|
+
if (node.type === "element") if (node.tagName === "pre") {
|
|
19
|
+
const code = node.children.find((c) => c.type === "element" && c.tagName === "code");
|
|
20
|
+
if (code) {
|
|
21
|
+
let lineNum = 1;
|
|
22
|
+
for (const child of code.children) if (child.type === "element" && child.tagName === "span") {
|
|
23
|
+
const cls = child.properties.className ?? child.properties.class;
|
|
24
|
+
if (Array.isArray(cls) ? cls.includes("line") : typeof cls === "string" && cls.split(" ").includes("line")) child.properties["data-line"] = String(lineNum++);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
} else for (const child of node.children) applyDataLineAttributes(child);
|
|
28
|
+
else if (node.type === "root") for (const child of node.children) applyDataLineAttributes(child);
|
|
29
|
+
}
|
|
30
|
+
function compile(input, options) {
|
|
31
|
+
const native = loadNativeBinding();
|
|
32
|
+
const plugins = options?.plugins ?? [];
|
|
33
|
+
const hastTransforms = plugins.filter((p) => Boolean(p.hastTransform)).map((p) => p.hastTransform);
|
|
34
|
+
const mdxJsTransforms = plugins.filter((p) => Boolean(p.mdxJsTransform)).map((p) => p.mdxJsTransform);
|
|
35
|
+
let mergedOpts = { ...options };
|
|
36
|
+
delete mergedOpts.plugins;
|
|
37
|
+
for (const plugin of plugins) if (plugin.options) mergedOpts = deepmerge(mergedOpts, plugin.options);
|
|
38
|
+
const hasHastTransforms = hastTransforms.length > 0;
|
|
39
|
+
const userRequestedOutputKind = options?.outputKind;
|
|
40
|
+
if (hasHastTransforms && userRequestedOutputKind !== "hast" && userRequestedOutputKind !== "mdxJs" && userRequestedOutputKind !== "mdast") mergedOpts.outputKind = "hast";
|
|
41
|
+
const rawResult = native.compile(input, mergedOpts);
|
|
42
|
+
let { output } = rawResult;
|
|
43
|
+
if (hasHastTransforms && userRequestedOutputKind !== "mdxJs" && userRequestedOutputKind !== "mdast") {
|
|
44
|
+
let hast;
|
|
45
|
+
try {
|
|
46
|
+
hast = JSON.parse(output);
|
|
47
|
+
} catch {
|
|
48
|
+
throw new Error("Failed to parse HAST output from native binding");
|
|
49
|
+
}
|
|
50
|
+
for (const transform of hastTransforms) hast = transform(hast);
|
|
51
|
+
if (options?.lineNumbers?.enabled) for (const child of hast.children) applyDataLineAttributes(child);
|
|
52
|
+
output = userRequestedOutputKind === "hast" ? JSON.stringify(hast) : native.stringifyHast(JSON.stringify(hast));
|
|
53
|
+
}
|
|
54
|
+
if (mdxJsTransforms.length > 0 && (userRequestedOutputKind === "mdxJs" || mergedOpts.outputKind === "mdxJs")) for (const transform of mdxJsTransforms) output = transform(output);
|
|
55
|
+
let frontmatter;
|
|
56
|
+
try {
|
|
57
|
+
frontmatter = JSON.parse(rawResult.frontmatter);
|
|
58
|
+
} catch {
|
|
59
|
+
frontmatter = {};
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
output,
|
|
63
|
+
sourcemap: rawResult.sourcemap ?? void 0,
|
|
64
|
+
frontmatter,
|
|
65
|
+
diagnostics: rawResult.diagnostics.map((d) => ({
|
|
66
|
+
level: d.level,
|
|
67
|
+
message: d.message,
|
|
68
|
+
start: d.start,
|
|
69
|
+
end: d.end
|
|
70
|
+
})),
|
|
71
|
+
stats: rawResult.stats,
|
|
72
|
+
toc: rawResult.toc ?? [],
|
|
73
|
+
readingTime: rawResult.readingTime ?? void 0,
|
|
74
|
+
excerpt: rawResult.excerpt ?? void 0
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
//#endregion
|
|
78
|
+
export { CompileError, ParseError, UnifastError, abbr, accessibleEmoji, addClasses, autolinkHeadings, breaks, cjk, codeImport, codeMeta, commentRemoval, compile, customHeadingId, definitionList, directive, emoji, escapeHtml, excerpt, externalLinks, extractLang, extractText, figure, findCodeChild, frontmatter, gfm, githubAlert, hastToHtml, imgLazyLoading, math, minify, readingTime, rubyAnnotation, sanitize, sectionize, smartypants, syntect, toc, treeSitter, visitHast, wikiLink };
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@unifast/node",
|
|
3
|
+
"version": "0.0.4",
|
|
4
|
+
"description": "Node.js binding for unifast Markdown/MDX compiler",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist",
|
|
7
|
+
"native"
|
|
8
|
+
],
|
|
9
|
+
"main": "./dist/index.cjs",
|
|
10
|
+
"module": "./dist/index.mjs",
|
|
11
|
+
"types": "./dist/index.d.cts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"import": "./dist/index.mjs",
|
|
15
|
+
"require": "./dist/index.cjs"
|
|
16
|
+
},
|
|
17
|
+
"./package.json": "./package.json"
|
|
18
|
+
},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"deepmerge": "^4.3.1",
|
|
24
|
+
"@unifast/core": "0.0.4"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/node": "^25.4.0",
|
|
28
|
+
"typescript": "^5.9.3",
|
|
29
|
+
"vitest": "^4.1.0"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsdown",
|
|
33
|
+
"lint:oxc": "oxlint -c ../../.oxlintrc.json .",
|
|
34
|
+
"lint:oxc:fix": "oxlint -c ../../.oxlintrc.json --fix .",
|
|
35
|
+
"typecheck": "tsc --noEmit",
|
|
36
|
+
"fmt:oxc": "oxfmt --write .",
|
|
37
|
+
"fmt:oxc:check": "oxfmt --check .",
|
|
38
|
+
"test": "vitest run",
|
|
39
|
+
"test:coverage": "vitest run --coverage --coverage.reporter=text --coverage.reporter=lcov"
|
|
40
|
+
}
|
|
41
|
+
}
|