@mralfarrakhan/svork 0.6.6 → 0.7.0-alpha

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/dist/index.d.mts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { PreprocessorGroup } from "svelte/compiler";
2
+ import { CompileArgs } from "@myriaddreamin/typst-ts-node-compiler";
2
3
 
3
4
  //#region node_modules/@types/unist/index.d.ts
4
5
  // ## Interfaces
@@ -3287,4 +3288,12 @@ type SvelteMarkdownOptions = {
3287
3288
  };
3288
3289
  declare const svelteMarkdown: (options?: SvelteMarkdownOptions) => PreprocessorGroup;
3289
3290
  //#endregion
3290
- export { SvelteMarkdownOptions, svelteMarkdown };
3291
+ //#region src/typst.d.ts
3292
+ type SvelteTypstOptions = {
3293
+ extensions?: string[];
3294
+ rehypePlugins?: PluggableList;
3295
+ compileArgs?: CompileArgs;
3296
+ };
3297
+ declare const svelteTypst: (options?: SvelteTypstOptions) => PreprocessorGroup;
3298
+ //#endregion
3299
+ export { SvelteMarkdownOptions, SvelteTypstOptions, svelteMarkdown, svelteTypst };
package/dist/index.mjs CHANGED
@@ -7,6 +7,7 @@ import remarkRehype from "remark-rehype";
7
7
  import rehypeRaw from "rehype-raw";
8
8
  import rehypeStringify from "rehype-stringify";
9
9
  import yaml from "js-yaml";
10
+ import { NodeCompiler } from "@myriaddreamin/typst-ts-node-compiler";
10
11
  //#region \0rolldown/runtime.js
11
12
  var __create = Object.create;
12
13
  var __defProp = Object.defineProperty;
@@ -2220,6 +2221,33 @@ function isUint8Array(value) {
2220
2221
  return Boolean(value && typeof value === "object" && "byteLength" in value && "byteOffset" in value);
2221
2222
  }
2222
2223
  //#endregion
2224
+ //#region src/shared.ts
2225
+ const escapeSvelteTextBraces = (value) => value.replace(/\{/g, "{").replace(/\}/g, "}");
2226
+ const revertDoubleEscapedBraces = (html) => html.replace(/({|{)/g, "{").replace(/(}|})/g, "}");
2227
+ function escapeBracesPlugin() {
2228
+ return (tree) => {
2229
+ const SKIP = new Set(["script", "style"]);
2230
+ const escapeProperties = (properties) => {
2231
+ if (!properties) return;
2232
+ for (const [key, value] of Object.entries(properties)) if (typeof value === "string") properties[key] = escapeSvelteTextBraces(value);
2233
+ else if (Array.isArray(value)) properties[key] = value.map((item) => typeof item === "string" ? escapeSvelteTextBraces(item) : item);
2234
+ };
2235
+ const visit = (node, ancestors) => {
2236
+ if (!node) return;
2237
+ if (node.type === "element") escapeProperties(node.properties);
2238
+ if (node.type === "text") {
2239
+ if (!ancestors.some((a) => a?.type === "element" && typeof a.tagName === "string" && SKIP.has(a.tagName)) && typeof node.value === "string" && (node.value.includes("{") || node.value.includes("}"))) node.value = escapeSvelteTextBraces(node.value);
2240
+ }
2241
+ for (const key of Object.keys(node)) {
2242
+ const child = node[key];
2243
+ if (Array.isArray(child)) for (const c of child) visit(c, ancestors.concat(node));
2244
+ else if (child && typeof child === "object" && child.type) visit(child, ancestors.concat(node));
2245
+ }
2246
+ };
2247
+ visit(tree, []);
2248
+ };
2249
+ }
2250
+ //#endregion
2223
2251
  //#region src/markdown.ts
2224
2252
  const genId = () => `SVELTE_${Math.random().toString(36).slice(2, 10)}_${Date.now().toString(36)}`;
2225
2253
  const escapeSvelteText = (value) => value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\{/g, "&#123;").replace(/\}/g, "&#125;");
@@ -2310,30 +2338,6 @@ const maskMarkdownCodeForSvelteParse = (source) => {
2310
2338
  }
2311
2339
  return chars.join("");
2312
2340
  };
2313
- const escapeSvelteTextBraces = (value) => value.replace(/\{/g, "&#123;").replace(/\}/g, "&#125;");
2314
- function escapeBracesPlugin() {
2315
- return (tree) => {
2316
- const SKIP = new Set(["script", "style"]);
2317
- const escapeProperties = (properties) => {
2318
- if (!properties) return;
2319
- for (const [key, value] of Object.entries(properties)) if (typeof value === "string") properties[key] = escapeSvelteTextBraces(value);
2320
- else if (Array.isArray(value)) properties[key] = value.map((item) => typeof item === "string" ? escapeSvelteTextBraces(item) : item);
2321
- };
2322
- const visit = (node, ancestors) => {
2323
- if (!node) return;
2324
- if (node.type === "element") escapeProperties(node.properties);
2325
- if (node.type === "text") {
2326
- if (!ancestors.some((a) => a?.type === "element" && typeof a.tagName === "string" && SKIP.has(a.tagName)) && typeof node.value === "string" && (node.value.includes("{") || node.value.includes("}"))) node.value = escapeSvelteTextBraces(node.value);
2327
- }
2328
- for (const key of Object.keys(node)) {
2329
- const child = node[key];
2330
- if (Array.isArray(child)) for (const c of child) visit(c, ancestors.concat(node));
2331
- else if (child && typeof child === "object" && child.type) visit(child, ancestors.concat(node));
2332
- }
2333
- };
2334
- visit(tree, []);
2335
- };
2336
- }
2337
2341
  const svelteMarkdown = (options) => {
2338
2342
  const hasWantedExt = (s) => (options?.extensions ?? [".md"]).some((e) => s.endsWith(e.trim()));
2339
2343
  const FRONTMATTER_REGEX = /^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)/;
@@ -2355,9 +2359,7 @@ const svelteMarkdown = (options) => {
2355
2359
  }
2356
2360
  const finalize = async (markdownSource, placeholderMap = /* @__PURE__ */ new Map()) => {
2357
2361
  const vfile = await mdCompiler.process(markdownSource);
2358
- let compiled = String(vfile);
2359
- compiled = compiled.replace(/(&amp;#123;|&#x26;#123;)/g, "&#123;").replace(/(&amp;#125;|&#x26;#125;)/g, "&#125;");
2360
- let restored = compiled;
2362
+ let restored = revertDoubleEscapedBraces(String(vfile));
2361
2363
  if (vfile.data?.fm && typeof vfile.data.fm === "object") metadata = {
2362
2364
  ...metadata,
2363
2365
  ...vfile.data.fm
@@ -2505,4 +2507,52 @@ const svelteMarkdown = (options) => {
2505
2507
  };
2506
2508
  };
2507
2509
  //#endregion
2508
- export { svelteMarkdown };
2510
+ //#region src/typst.ts
2511
+ const svelteTypst = (options) => {
2512
+ let compiler = null;
2513
+ const getCompiler = () => {
2514
+ if (!compiler) compiler = NodeCompiler.create(options?.compileArgs ?? {});
2515
+ return compiler;
2516
+ };
2517
+ const hasWantedExt = (s) => (options?.extensions ?? [".typ"]).some((e) => s.endsWith(e.trim()));
2518
+ const rehypeProcessor = unified().use(options?.rehypePlugins ?? []).use(escapeBracesPlugin).use(rehypeStringify, { allowDangerousHtml: true });
2519
+ return {
2520
+ name: "svelteTypst",
2521
+ markup: async ({ content, filename }) => {
2522
+ if (!filename || !hasWantedExt(filename)) return;
2523
+ const c = getCompiler();
2524
+ const compiled = c.compileHtml({ mainFileContent: content });
2525
+ if (compiled.hasError()) {
2526
+ compiled.printErrors();
2527
+ throw new Error(`[svelteTypst] Compilation failed for ${filename}`);
2528
+ }
2529
+ const doc = compiled.result;
2530
+ let metadata = {};
2531
+ try {
2532
+ const queryResult = c.query(doc, {
2533
+ selector: "<frontmatter>",
2534
+ field: "value"
2535
+ });
2536
+ if (Array.isArray(queryResult) && queryResult.length > 0) {
2537
+ const value = queryResult[0];
2538
+ if (value !== null && typeof value === "object" && !Array.isArray(value)) metadata = value;
2539
+ }
2540
+ } catch {}
2541
+ const htmlExec = c.tryHtml(doc);
2542
+ if (htmlExec.hasError()) {
2543
+ htmlExec.printErrors();
2544
+ throw new Error(`[svelteTypst] HTML rendering failed for ${filename}`);
2545
+ }
2546
+ const docHast = htmlExec.result.hast();
2547
+ const bodyRoot = {
2548
+ type: "root",
2549
+ children: (docHast.children?.find((n) => n.tagName === "body"))?.children ?? docHast.children ?? []
2550
+ };
2551
+ const transformed = await rehypeProcessor.run(bodyRoot);
2552
+ const html = revertDoubleEscapedBraces(rehypeProcessor.stringify(transformed));
2553
+ return { code: `<script module lang="ts">${`\nexport const metadata = ${JSON.stringify(metadata)};\n`}<\/script>\n${html}` };
2554
+ }
2555
+ };
2556
+ };
2557
+ //#endregion
2558
+ export { svelteMarkdown, svelteTypst };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mralfarrakhan/svork",
3
3
  "type": "module",
4
- "version": "0.6.6",
4
+ "version": "0.7.0-alpha",
5
5
  "description": "Svelte utilities for writing blog",
6
6
  "author": "mralfarrakhan <alfarrakhan@gmail.com",
7
7
  "license": "AGPL-3.0-or-later",
@@ -41,6 +41,7 @@
41
41
  "prepare": "bunx tsdown --dst"
42
42
  },
43
43
  "devDependencies": {
44
+ "@myriaddreamin/typst-ts-node-compiler": "^0.7.0-rc2",
44
45
  "@shikijs/rehype": "^4.1.0",
45
46
  "@types/js-yaml": "^4.0.9",
46
47
  "@types/node": "^25.9.1",
@@ -57,7 +58,13 @@
57
58
  "vitest": "^4.1.7"
58
59
  },
59
60
  "peerDependencies": {
60
- "svelte": ">=5.0.0"
61
+ "svelte": ">=5.0.0",
62
+ "@myriaddreamin/typst-ts-node-compiler": ">=0.7.0"
63
+ },
64
+ "peerDependenciesMeta": {
65
+ "@myriaddreamin/typst-ts-node-compiler": {
66
+ "optional": true
67
+ }
61
68
  },
62
69
  "dependencies": {
63
70
  "js-yaml": "^4.1.1",