topotopo 0.4.0 → 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/NOTICE +10 -10
- package/README.md +460 -117
- package/dist/cli.js +323 -146
- package/dist/config.d.ts +48 -0
- package/dist/config.js +170 -0
- package/dist/core/bake.d.ts +14 -3
- package/dist/core/bake.js +153 -4
- package/dist/core/bevel.d.ts +3 -1
- package/dist/core/bevel.js +76 -3
- package/dist/core/decimate.d.ts +65 -0
- package/dist/core/decimate.js +409 -3
- package/dist/core/erosion.d.ts +37 -0
- package/dist/core/erosion.js +41 -1
- package/dist/core/geometry.d.ts +5 -1
- package/dist/core/geometry.js +203 -3
- package/dist/core/heightfield.d.ts +27 -4
- package/dist/core/heightfield.js +54 -16
- package/dist/core/intrinsics.d.ts +7 -0
- package/dist/core/intrinsics.js +94 -0
- package/dist/core/manifold.d.ts +12 -1
- package/dist/core/manifold.js +78 -6
- package/dist/core/materials.d.ts +7 -0
- package/dist/core/materials.js +45 -0
- package/dist/core/noise.d.ts +9 -0
- package/dist/core/noise.js +10 -1
- package/dist/core/polymerge.d.ts +21 -0
- package/dist/core/polymerge.js +327 -0
- package/dist/core/repair.d.ts +2 -0
- package/dist/core/repair.js +2 -0
- package/dist/core/report.d.ts +70 -0
- package/dist/core/report.js +275 -0
- package/dist/core/scene.js +2 -1
- package/dist/core/sha256.d.ts +4 -0
- package/dist/core/sha256.js +69 -0
- package/dist/core/shapes.d.ts +3 -0
- package/dist/core/shapes.js +34 -0
- package/dist/core/snippet-lib.d.ts +14 -1
- package/dist/core/snippet-lib.js +39 -13
- package/dist/core/tangents.d.ts +2 -0
- package/dist/core/tangents.js +5 -1
- package/dist/core/texnoise.d.ts +6 -1
- package/dist/core/texnoise.js +6 -2
- package/dist/core/texsdf.d.ts +131 -0
- package/dist/core/texsdf.js +637 -0
- package/dist/core/texture.d.ts +163 -9
- package/dist/core/texture.js +844 -20
- package/dist/core/topology.d.ts +10 -0
- package/dist/core/topology.js +168 -0
- package/dist/core/unwrap.d.ts +72 -0
- package/dist/core/unwrap.js +800 -0
- package/dist/core/usercode.d.ts +27 -0
- package/dist/core/usercode.js +87 -0
- package/dist/core/uv.d.ts +4 -18
- package/dist/core/uv.js +35 -188
- package/dist/core/uvislands.d.ts +62 -0
- package/dist/core/uvislands.js +574 -0
- package/dist/core/uvoverlap.d.ts +58 -0
- package/dist/core/uvoverlap.js +220 -0
- package/dist/core/uvpack.d.ts +64 -0
- package/dist/core/uvpack.js +628 -0
- package/dist/core/uvreport.d.ts +61 -0
- package/dist/core/uvreport.js +207 -143
- package/dist/core/wrangle.d.ts +6 -1
- package/dist/core/wrangle.js +27 -16
- package/dist/core/xatlas.d.ts +16 -1
- package/dist/core/xatlas.js +66 -62
- package/dist/engines.d.ts +63 -0
- package/dist/engines.js +180 -0
- package/dist/explain.d.ts +4 -3
- package/dist/explain.js +129 -12
- package/dist/graph/engine.d.ts +92 -29
- package/dist/graph/engine.js +298 -148
- package/dist/graph/expr.d.ts +6 -3
- package/dist/graph/expr.js +18 -8
- package/dist/graph/outputs.d.ts +23 -0
- package/dist/graph/outputs.js +41 -0
- package/dist/graph/packages.d.ts +72 -0
- package/dist/graph/packages.js +261 -0
- package/dist/graph/parallel.d.ts +3 -1
- package/dist/graph/parallel.js +4 -1
- package/dist/graph/params.d.ts +6 -0
- package/dist/graph/params.js +34 -7
- package/dist/graph/scope.d.ts +55 -0
- package/dist/graph/scope.js +14 -0
- package/dist/graph/types.d.ts +75 -12
- package/dist/graph/types.js +4 -2
- package/dist/graph/vars.d.ts +16 -0
- package/dist/graph/vars.js +80 -0
- package/dist/host/artifacts.d.ts +10 -1
- package/dist/host/artifacts.js +37 -6
- package/dist/host/cook.d.ts +241 -0
- package/dist/host/cook.js +844 -0
- package/dist/host/export-targets.d.ts +42 -5
- package/dist/host/export-targets.js +72 -13
- package/dist/host/godot-commands.d.ts +4 -0
- package/dist/host/godot-commands.js +21 -0
- package/dist/host/godot.d.ts +183 -0
- package/dist/host/godot.js +916 -0
- package/dist/host/node.d.ts +12 -7
- package/dist/host/node.js +27 -9
- package/dist/host/pool.js +51 -2
- package/dist/host/sidecars.d.ts +8 -0
- package/dist/host/sidecars.js +50 -0
- package/dist/host/types.d.ts +19 -6
- package/dist/host/version.d.ts +1 -0
- package/dist/host/version.js +3 -0
- package/dist/host/worker.js +5 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/inspect.d.ts +80 -2
- package/dist/inspect.js +265 -30
- package/dist/io/common.d.ts +25 -5
- package/dist/io/export.js +2 -1
- package/dist/io/files.d.ts +23 -4
- package/dist/io/files.js +60 -8
- package/dist/io/gltf.js +134 -47
- package/dist/io/heightmap.d.ts +3 -0
- package/dist/io/heightmap.js +1 -0
- package/dist/io/import.js +10 -0
- package/dist/io/inspect-scene.d.ts +55 -0
- package/dist/io/inspect-scene.js +155 -0
- package/dist/io/kinds.d.ts +81 -0
- package/dist/io/kinds.js +163 -0
- package/dist/io/points.d.ts +2 -1
- package/dist/io/points.js +3 -1
- package/dist/io/sidecars.d.ts +24 -0
- package/dist/io/sidecars.js +57 -0
- package/dist/io/source.d.ts +65 -0
- package/dist/io/source.js +112 -0
- package/dist/io/targets.d.ts +47 -3
- package/dist/io/targets.js +148 -13
- package/dist/io/textures.js +21 -1
- package/dist/io/unreal.js +5 -1
- package/dist/mcp/docs.d.ts +10 -1
- package/dist/mcp/docs.js +786 -143
- package/dist/mcp/inputs.d.ts +12 -5
- package/dist/mcp/inputs.js +50 -13
- package/dist/mcp/server.js +56 -27
- package/dist/mcp/tools.d.ts +14 -7
- package/dist/mcp/tools.js +196 -103
- package/dist/mcp/verify.d.ts +2 -2
- package/dist/mcp/verify.js +1 -1
- package/dist/mcp/workspace.d.ts +15 -0
- package/dist/mcp/workspace.js +349 -0
- package/dist/nodes/attributes.js +198 -1
- package/dist/nodes/bake.js +151 -47
- package/dist/nodes/beam.d.ts +1 -0
- package/dist/nodes/beam.js +201 -0
- package/dist/nodes/bevel.js +51 -3
- package/dist/nodes/cloth.js +2 -0
- package/dist/nodes/code.js +21 -9
- package/dist/nodes/curves.js +433 -93
- package/dist/nodes/flow.js +53 -33
- package/dist/nodes/generate.js +12 -38
- package/dist/nodes/heightfield.js +248 -77
- package/dist/nodes/imageops.js +16 -19
- package/dist/nodes/index.d.ts +1 -0
- package/dist/nodes/index.js +1 -0
- package/dist/nodes/material.js +89 -24
- package/dist/nodes/measure.js +43 -17
- package/dist/nodes/particles.js +2 -0
- package/dist/nodes/pcg.js +6 -5
- package/dist/nodes/procedural.js +4 -3
- package/dist/nodes/remesh.js +71 -15
- package/dist/nodes/scene.js +4 -2
- package/dist/nodes/sdf.js +10 -5
- package/dist/nodes/solid.js +20 -5
- package/dist/nodes/splines.d.ts +15 -1
- package/dist/nodes/splines.js +112 -105
- package/dist/nodes/texture.d.ts +21 -6
- package/dist/nodes/texture.js +1318 -185
- package/dist/nodes/topology.d.ts +7 -0
- package/dist/nodes/topology.js +133 -6
- package/dist/nodes/transform.js +6 -2
- package/dist/nodes/uv.js +192 -174
- package/dist/nodes/wasmcall.js +5 -1
- package/dist/open.d.ts +19 -0
- package/dist/open.js +12 -0
- package/dist/package.d.ts +288 -0
- package/dist/package.js +772 -0
- package/dist/packages/check.d.ts +32 -0
- package/dist/packages/check.js +169 -0
- package/dist/packages/commands.d.ts +57 -0
- package/dist/packages/commands.js +537 -0
- package/dist/packages/registry.d.ts +796 -0
- package/dist/packages/registry.js +2998 -0
- package/dist/packages/update.d.ts +124 -0
- package/dist/packages/update.js +476 -0
- package/dist/render/options.d.ts +2 -0
- package/dist/render/options.js +44 -5
- package/dist/render/preview.d.ts +15 -4
- package/dist/render/preview.js +6 -4
- package/dist/render/raster.d.ts +66 -5
- package/dist/render/raster.js +506 -138
- package/dist/render/texview.d.ts +6 -0
- package/dist/render/texview.js +27 -0
- package/dist/scan.d.ts +61 -0
- package/dist/scan.js +228 -0
- package/dist/session.d.ts +311 -0
- package/dist/session.js +849 -0
- package/dist/viewer/app/api.js +48 -6
- package/dist/viewer/app/cooking.js +78 -0
- package/dist/viewer/app/embed.js +27 -7
- package/dist/viewer/app/export-popover.js +118 -16
- package/dist/viewer/app/grid.js +5 -1
- package/dist/viewer/app/guides.js +122 -0
- package/dist/viewer/app/keymap.js +6 -3
- package/dist/viewer/app/layers.js +4 -4
- package/dist/viewer/app/main.js +185 -52
- package/dist/viewer/app/markers.js +60 -0
- package/dist/viewer/app/outliner.js +72 -27
- package/dist/viewer/app/output-info.js +45 -0
- package/dist/viewer/app/outputs.css +147 -8
- package/dist/viewer/app/outputs.js +168 -54
- package/dist/viewer/app/packages.js +614 -0
- package/dist/viewer/app/props.js +176 -52
- package/dist/viewer/app/scene.js +30 -5
- package/dist/viewer/app/select.js +4 -3
- package/dist/viewer/app/status.js +12 -15
- package/dist/viewer/app/style.css +65 -7
- package/dist/viewer/app/vars.js +47 -0
- package/dist/viewer/app/welcome.js +72 -0
- package/dist/viewer/graph-file.d.ts +62 -0
- package/dist/viewer/graph-file.js +175 -0
- package/dist/viewer/index.html +23 -10
- package/dist/viewer/json-edit.d.ts +13 -0
- package/dist/viewer/json-edit.js +274 -0
- package/dist/viewer/outputs.d.ts +49 -5
- package/dist/viewer/outputs.js +264 -56
- package/dist/viewer/packages.d.ts +121 -0
- package/dist/viewer/packages.js +584 -0
- package/dist/viewer/server.d.ts +14 -9
- package/dist/viewer/server.js +266 -28
- package/dist/web/app/web.css +419 -0
- package/dist/web/app/web.js +188 -0
- package/dist/{gallery → web}/bake.d.ts +4 -4
- package/dist/{gallery → web}/bake.js +24 -15
- package/dist/web/folder-page.d.ts +101 -0
- package/dist/web/folder-page.js +146 -0
- package/dist/web/folders.d.ts +280 -0
- package/dist/web/folders.js +970 -0
- package/dist/web/library.d.ts +91 -0
- package/dist/web/library.js +228 -0
- package/dist/web/markdown.d.ts +45 -0
- package/dist/web/markdown.js +677 -0
- package/dist/web/pages.d.ts +258 -0
- package/dist/web/pages.js +301 -0
- package/dist/web/server.d.ts +24 -0
- package/dist/web/server.js +1068 -0
- package/dist/workspace.d.ts +88 -118
- package/dist/workspace.js +358 -460
- package/integrations/godot/addons/topotopo/LICENSE +202 -0
- package/integrations/godot/addons/topotopo/README.md +57 -0
- package/integrations/godot/addons/topotopo/context_menu.gd +43 -0
- package/integrations/godot/addons/topotopo/dock.gd +266 -0
- package/integrations/godot/addons/topotopo/dock.tscn +72 -0
- package/integrations/godot/addons/topotopo/export_filter.gd +72 -0
- package/integrations/godot/addons/topotopo/gltf_instancing.gd +131 -0
- package/integrations/godot/addons/topotopo/icons/topotopo.svg +1 -0
- package/integrations/godot/addons/topotopo/inspector.gd +38 -0
- package/integrations/godot/addons/topotopo/instances_post_import.gd +111 -0
- package/integrations/godot/addons/topotopo/package.gd +23 -0
- package/integrations/godot/addons/topotopo/package_importer.gd +187 -0
- package/integrations/godot/addons/topotopo/plugin.cfg +7 -0
- package/integrations/godot/addons/topotopo/plugin.gd +970 -0
- package/integrations/godot/addons/topotopo/record.gd +435 -0
- package/integrations/godot/addons/topotopo/runner.gd +458 -0
- package/integrations/godot/addons/topotopo/tools/check.gd +42 -0
- package/integrations/godot/addons/topotopo/watcher.gd +199 -0
- package/kernels/pkg/README.md +2 -1
- package/kernels/pkg-simd/README.md +2 -1
- package/library/architecture/versions/1.0/architecture.topo +12 -0
- package/{shelf → library/architecture/versions/1.0/graphs}/arch.json +3 -3
- package/{shelf → library/architecture/versions/1.0/graphs}/beam_truss.json +6 -6
- package/{shelf → library/architecture/versions/1.0/graphs}/brick_wall.json +8 -8
- package/{shelf → library/architecture/versions/1.0/graphs}/door_frame.json +3 -3
- package/{shelf → library/architecture/versions/1.0/graphs}/hex_grid_floor.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/house.json +8 -8
- package/library/architecture/versions/1.0/graphs/lineup.json +42 -0
- package/{shelf → library/architecture/versions/1.0/graphs}/pillar.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/railing.json +5 -5
- package/{shelf → library/architecture/versions/1.0/graphs}/roof_gable.json +2 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/roof_hip.json +2 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/roof_skeleton.json +2 -1
- package/{shelf → library/architecture/versions/1.0/graphs}/scaffold.json +4 -4
- package/{shelf → library/architecture/versions/1.0/graphs}/spiral_stairs.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/stairs.json +6 -6
- package/{shelf → library/architecture/versions/1.0/graphs}/tile_floor.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/wall_segment.json +10 -10
- package/{shelf → library/architecture/versions/1.0/graphs}/window_frame.json +2 -2
- package/library/architecture/versions/1.0/version.json +1119 -0
- package/{shelf → library/nature/versions/1.0/graphs}/bush.json +2 -2
- package/{shelf → library/nature/versions/1.0/graphs}/cliff_block.json +4 -3
- package/{shelf → library/nature/versions/1.0/graphs}/forest_patch.json +5 -5
- package/{shelf → library/nature/versions/1.0/graphs}/grass_clumps.json +2 -2
- package/library/nature/versions/1.0/graphs/lineup.json +24 -0
- package/{shelf → library/nature/versions/1.0/graphs}/log.json +6 -6
- package/{shelf → library/nature/versions/1.0/graphs}/rock.json +5 -4
- package/{shelf → library/nature/versions/1.0/graphs}/scatter_rocks.json +5 -5
- package/{shelf → library/nature/versions/1.0/graphs}/tree_lowpoly.json +3 -2
- package/library/nature/versions/1.0/nature.topo +12 -0
- package/library/nature/versions/1.0/version.json +534 -0
- package/{shelf → library/props/versions/1.0/graphs}/barrel.json +5 -4
- package/{shelf → library/props/versions/1.0/graphs}/crate.json +3 -3
- package/library/props/versions/1.0/graphs/lineup.json +18 -0
- package/{shelf → library/props/versions/1.0/graphs}/pallet.json +4 -3
- package/{shelf → library/props/versions/1.0/graphs}/pipe_run.json +3 -3
- package/{shelf → library/props/versions/1.0/graphs}/sandbag_wall.json +5 -5
- package/library/props/versions/1.0/props.topo +12 -0
- package/library/props/versions/1.0/version.json +336 -0
- package/{shelf → library/street/versions/1.0/graphs}/bench.json +5 -5
- package/{shelf → library/street/versions/1.0/graphs}/bollard.json +2 -1
- package/{shelf → library/street/versions/1.0/graphs}/cable.json +4 -4
- package/{shelf → library/street/versions/1.0/graphs}/fence.json +7 -7
- package/{shelf → library/street/versions/1.0/graphs}/lamp_post.json +2 -2
- package/library/street/versions/1.0/graphs/lineup.json +22 -0
- package/{shelf → library/street/versions/1.0/graphs}/road_strip.json +2 -2
- package/{shelf → library/street/versions/1.0/graphs}/sign_post.json +1 -1
- package/library/street/versions/1.0/street.topo +12 -0
- package/library/street/versions/1.0/version.json +421 -0
- package/package.json +17 -8
- package/dist/gallery/app/gallery.css +0 -100
- package/dist/gallery/app/gallery.js +0 -80
- package/dist/gallery/cli.d.ts +0 -4
- package/dist/gallery/cli.js +0 -157
- package/dist/gallery/library.d.ts +0 -124
- package/dist/gallery/library.js +0 -366
- package/dist/gallery/pages.d.ts +0 -94
- package/dist/gallery/pages.js +0 -148
- package/dist/gallery/server.d.ts +0 -21
- package/dist/gallery/server.js +0 -391
- package/dist/graph/migrate.d.ts +0 -23
- package/dist/graph/migrate.js +0 -58
- package/dist/mcp/library.d.ts +0 -12
- package/dist/mcp/library.js +0 -159
- package/shelf/index.json +0 -190
|
@@ -0,0 +1,677 @@
|
|
|
1
|
+
// A small markdown renderer for the notes on the web app's folder pages: front matter (a "---" block of key: value
|
|
2
|
+
// lines at the top, shown as a small table of properties), headings (ATX and setext), paragraphs, lists (nested,
|
|
3
|
+
// ordered, task items), block quotes, fenced and indented code, thematic breaks, GFM tables, emphasis, strong,
|
|
4
|
+
// strikethrough, code spans, links and images (inline, reference and bare http(s) URLs). No raw HTML: every character
|
|
5
|
+
// of the source reaches the page escaped. Each link and image goes through a callback that decides how it shows (the
|
|
6
|
+
// web app turns them into players, cards and struck-through missing files: src/web/folders.ts), told whether the link
|
|
7
|
+
// stands alone in its paragraph; what it returns to show below the paragraph follows it. Without a callback only
|
|
8
|
+
// http(s), mailto, relative and #fragment links are links, anything else (javascript:, data: …) stays text.
|
|
9
|
+
// Pure: no file system.
|
|
10
|
+
export const escHtml = (s) => s.replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c]);
|
|
11
|
+
/** A heading's anchor: lower case, letters, digits and dashes. */
|
|
12
|
+
export const slug = (s) => s.toLowerCase().replace(/<[^>]*>/g, '').replace(/&[a-z#0-9]+;/g, '').replace(/[^\p{L}\p{N}\s_-]/gu, '').trim().replace(/[\s]+/g, '-');
|
|
13
|
+
/** Only these destinations become links by default. */
|
|
14
|
+
export const safeHref = (href) => /^(https?:|mailto:)/i.test(href) || !/^[a-z][a-z0-9+.-]*:/i.test(href);
|
|
15
|
+
const FENCE = /^ {0,3}(`{3,}|~{3,})\s*([^`]*)$/;
|
|
16
|
+
const ATX = /^ {0,3}(#{1,6})(?:[ \t]+(.*?))?(?:[ \t]+#+)?[ \t]*$/;
|
|
17
|
+
const HR = /^ {0,3}([-*_])(?:[ \t]*\1){2,}[ \t]*$/;
|
|
18
|
+
const QUOTE = /^ {0,3}> ?/;
|
|
19
|
+
const ITEM = /^( {0,3})([-*+]|\d{1,9}[.)])([ \t]+|$)(.*)$/;
|
|
20
|
+
const TABLE_DELIM = /^ {0,3}\|?[ \t]*:?-+:?[ \t]*(\|[ \t]*:?-+:?[ \t]*)*\|?[ \t]*$/;
|
|
21
|
+
const REF_DEF = /^ {0,3}\[([^\]]{1,999})\]:[ \t]*<?([^\s>]+)>?(?:[ \t]+(?:"([^"]*)"|'([^']*)'|\(([^)]*)\)))?[ \t]*$/;
|
|
22
|
+
const blank = (s) => /^[ \t]*$/.test(s);
|
|
23
|
+
const indentOf = (s) => s.length - s.replace(/^ +/, '').length;
|
|
24
|
+
/** A line that starts a block other than a paragraph (so it ends one). */
|
|
25
|
+
const startsBlock = (s) => FENCE.test(s) || ATX.test(s) || HR.test(s) || QUOTE.test(s) || ITEM.test(s);
|
|
26
|
+
/** Table cells of a row: split on unescaped pipes outside code spans, outer pipes dropped. */
|
|
27
|
+
function cells(row) {
|
|
28
|
+
let s = row.trim();
|
|
29
|
+
if (s.startsWith('|'))
|
|
30
|
+
s = s.slice(1);
|
|
31
|
+
if (s.endsWith('|') && !s.endsWith('\\|'))
|
|
32
|
+
s = s.slice(0, -1);
|
|
33
|
+
const out = [];
|
|
34
|
+
let cur = '', tick = 0;
|
|
35
|
+
for (let i = 0; i < s.length; i++) {
|
|
36
|
+
const c = s[i];
|
|
37
|
+
if (c === '\\' && s[i + 1] === '|') {
|
|
38
|
+
cur += '|';
|
|
39
|
+
i++;
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (c === '`')
|
|
43
|
+
tick ^= 1;
|
|
44
|
+
if (c === '|' && !tick) {
|
|
45
|
+
out.push(cur.trim());
|
|
46
|
+
cur = '';
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
cur += c;
|
|
50
|
+
}
|
|
51
|
+
out.push(cur.trim());
|
|
52
|
+
return out;
|
|
53
|
+
}
|
|
54
|
+
function parseBlocks(lines, refs, depth = 0) {
|
|
55
|
+
const out = [];
|
|
56
|
+
let i = 0;
|
|
57
|
+
const n = lines.length;
|
|
58
|
+
while (i < n) {
|
|
59
|
+
const line = lines[i];
|
|
60
|
+
if (blank(line)) {
|
|
61
|
+
i++;
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (depth > 24) {
|
|
65
|
+
out.push({ t: 'p', text: lines.slice(i).join('\n') });
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
// fenced code
|
|
69
|
+
const f = FENCE.exec(line);
|
|
70
|
+
if (f) {
|
|
71
|
+
const fence = f[1], ind = indentOf(line), body = [];
|
|
72
|
+
i++;
|
|
73
|
+
while (i < n && !new RegExp(`^ {0,3}${fence[0] === '`' ? '`' : '~'}{${fence.length},}[ \\t]*$`).test(lines[i])) {
|
|
74
|
+
body.push(lines[i].replace(new RegExp(`^ {0,${ind}}`), ''));
|
|
75
|
+
i++;
|
|
76
|
+
}
|
|
77
|
+
i++;
|
|
78
|
+
out.push({ t: 'code', info: f[2].trim().split(/\s+/)[0] ?? '', text: body.join('\n') });
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
const h = ATX.exec(line);
|
|
82
|
+
if (h) {
|
|
83
|
+
out.push({ t: 'h', level: h[1].length, text: (h[2] ?? '').trim() });
|
|
84
|
+
i++;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (HR.test(line)) {
|
|
88
|
+
out.push({ t: 'hr' });
|
|
89
|
+
i++;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (QUOTE.test(line)) {
|
|
93
|
+
const body = [];
|
|
94
|
+
while (i < n && !blank(lines[i])) {
|
|
95
|
+
if (QUOTE.test(lines[i]))
|
|
96
|
+
body.push(lines[i].replace(QUOTE, ''));
|
|
97
|
+
else if (startsBlock(lines[i]))
|
|
98
|
+
break;
|
|
99
|
+
else
|
|
100
|
+
body.push(lines[i]); // a lazy continuation line of its paragraph
|
|
101
|
+
i++;
|
|
102
|
+
}
|
|
103
|
+
out.push({ t: 'quote', blocks: parseBlocks(body, refs, depth + 1) });
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
const it = ITEM.exec(line);
|
|
107
|
+
if (it) {
|
|
108
|
+
const ordered = /\d/.test(it[2]);
|
|
109
|
+
const kind = ordered ? it[2].slice(-1) : it[2];
|
|
110
|
+
const items = [];
|
|
111
|
+
let loose = false;
|
|
112
|
+
/** an item of this list: the same bullet, or numbers with the same delimiter */
|
|
113
|
+
const same = (l) => {
|
|
114
|
+
const m = l === undefined ? null : ITEM.exec(l);
|
|
115
|
+
return m && /\d/.test(m[2]) === ordered && (ordered ? m[2].slice(-1) : m[2]) === kind ? m : null;
|
|
116
|
+
};
|
|
117
|
+
while (i < n) {
|
|
118
|
+
const m = same(lines[i]);
|
|
119
|
+
if (!m)
|
|
120
|
+
break;
|
|
121
|
+
// content starts after the marker and its spaces (one space when the item starts with indented code or nothing)
|
|
122
|
+
const spaces = m[3].length;
|
|
123
|
+
const width = m[1].length + m[2].length + (spaces >= 1 && spaces <= 4 && m[4] !== '' ? spaces : 1);
|
|
124
|
+
const body = [m[4]];
|
|
125
|
+
i++;
|
|
126
|
+
let sawBlank = false;
|
|
127
|
+
while (i < n) {
|
|
128
|
+
const l = lines[i];
|
|
129
|
+
if (blank(l)) {
|
|
130
|
+
body.push('');
|
|
131
|
+
sawBlank = true;
|
|
132
|
+
i++;
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (indentOf(l) >= width) {
|
|
136
|
+
body.push(l.slice(width));
|
|
137
|
+
if (sawBlank)
|
|
138
|
+
loose = true;
|
|
139
|
+
sawBlank = false;
|
|
140
|
+
i++;
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
if (sawBlank)
|
|
144
|
+
break;
|
|
145
|
+
if (startsBlock(l))
|
|
146
|
+
break;
|
|
147
|
+
body.push(l.trim()); // a lazy continuation of the item's paragraph
|
|
148
|
+
i++;
|
|
149
|
+
}
|
|
150
|
+
while (body.length && blank(body[body.length - 1]))
|
|
151
|
+
body.pop();
|
|
152
|
+
if (sawBlank && same(lines[i]))
|
|
153
|
+
loose = true;
|
|
154
|
+
let task = null;
|
|
155
|
+
const tm = /^\[([ xX])\][ \t]+/.exec(body[0] ?? '');
|
|
156
|
+
if (tm) {
|
|
157
|
+
task = tm[1] !== ' ';
|
|
158
|
+
body[0] = body[0].slice(tm[0].length);
|
|
159
|
+
}
|
|
160
|
+
items.push({ task, blocks: parseBlocks(body, refs, depth + 1) });
|
|
161
|
+
if (sawBlank && !same(lines[i]))
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
out.push({ t: 'list', ordered, start: ordered ? Number.parseInt(it[2], 10) : 1, loose, items });
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
// a table: a row with pipes over a delimiter row with as many cells
|
|
168
|
+
if (line.includes('|') && i + 1 < n && TABLE_DELIM.test(lines[i + 1]) && lines[i + 1].includes('-')) {
|
|
169
|
+
const head = cells(line), delim = cells(lines[i + 1]);
|
|
170
|
+
if (head.length === delim.length) {
|
|
171
|
+
const align = delim.map((d) => (d.startsWith(':') && d.endsWith(':') ? 'center' : d.endsWith(':') ? 'right' : d.startsWith(':') ? 'left' : null));
|
|
172
|
+
const rows = [];
|
|
173
|
+
i += 2;
|
|
174
|
+
while (i < n && !blank(lines[i]) && lines[i].includes('|') && !startsBlock(lines[i])) {
|
|
175
|
+
const r = cells(lines[i]);
|
|
176
|
+
rows.push(head.map((_, k) => r[k] ?? ''));
|
|
177
|
+
i++;
|
|
178
|
+
}
|
|
179
|
+
out.push({ t: 'table', align, head, rows });
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
// indented code
|
|
184
|
+
if (/^( {4}|\t)/.test(line)) {
|
|
185
|
+
const body = [];
|
|
186
|
+
while (i < n && (/^( {4}|\t)/.test(lines[i]) || blank(lines[i]))) {
|
|
187
|
+
body.push(lines[i].replace(/^( {4}|\t)/, ''));
|
|
188
|
+
i++;
|
|
189
|
+
}
|
|
190
|
+
while (body.length && blank(body[body.length - 1]))
|
|
191
|
+
body.pop();
|
|
192
|
+
out.push({ t: 'code', info: '', text: body.join('\n') });
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
// reference definitions at the start of a paragraph
|
|
196
|
+
const d = REF_DEF.exec(line);
|
|
197
|
+
if (d) {
|
|
198
|
+
const key = d[1].trim().toLowerCase().replace(/\s+/g, ' ');
|
|
199
|
+
if (!refs.has(key))
|
|
200
|
+
refs.set(key, { href: d[2], title: d[3] ?? d[4] ?? d[5] ?? '' });
|
|
201
|
+
i++;
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
// a paragraph, which a setext underline makes a heading
|
|
205
|
+
const para = [line.trim()];
|
|
206
|
+
i++;
|
|
207
|
+
let setext = 0;
|
|
208
|
+
while (i < n && !blank(lines[i])) {
|
|
209
|
+
if (/^ {0,3}=+[ \t]*$/.test(lines[i])) {
|
|
210
|
+
setext = 1;
|
|
211
|
+
i++;
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
if (/^ {0,3}-+[ \t]*$/.test(lines[i])) {
|
|
215
|
+
setext = 2;
|
|
216
|
+
i++;
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
if (startsBlock(lines[i]))
|
|
220
|
+
break;
|
|
221
|
+
if (lines[i].includes('|') && i + 1 < n && TABLE_DELIM.test(lines[i + 1]) && lines[i + 1].includes('-'))
|
|
222
|
+
break;
|
|
223
|
+
para.push(lines[i].replace(/^[ \t]+/, ''));
|
|
224
|
+
i++;
|
|
225
|
+
}
|
|
226
|
+
const text = para.join('\n');
|
|
227
|
+
out.push(setext ? { t: 'h', level: setext, text: text.trim() } : { t: 'p', text });
|
|
228
|
+
}
|
|
229
|
+
return out;
|
|
230
|
+
}
|
|
231
|
+
// --------------------------------------------------------------------------------------------------------- inlines
|
|
232
|
+
const PUNCT = /[!-/:-@[-`{-~]/;
|
|
233
|
+
const isSpace = (c) => c === undefined || /\s/.test(c);
|
|
234
|
+
const isPunct = (c) => c !== undefined && /[\p{P}\p{S}]/u.test(c);
|
|
235
|
+
const isAlnum = (c) => c !== undefined && /[\p{L}\p{N}]/u.test(c);
|
|
236
|
+
const marksOf = new Map();
|
|
237
|
+
function marks(s) {
|
|
238
|
+
const have = marksOf.get(s);
|
|
239
|
+
if (have)
|
|
240
|
+
return have;
|
|
241
|
+
const n = s.length, code = new Int32Array(n).fill(-1), bracket = new Int32Array(n).fill(-1);
|
|
242
|
+
const runs = [];
|
|
243
|
+
for (let k = 0; k < n;) {
|
|
244
|
+
if (s[k] !== '`') {
|
|
245
|
+
k++;
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
let e = k;
|
|
249
|
+
while (s[e] === '`')
|
|
250
|
+
e++;
|
|
251
|
+
runs.push([k, e - k]);
|
|
252
|
+
k = e;
|
|
253
|
+
}
|
|
254
|
+
// a run closes at the next run of its length
|
|
255
|
+
const next = new Map();
|
|
256
|
+
for (let r = runs.length - 1; r >= 0; r--) {
|
|
257
|
+
const [at, len] = runs[r], c = next.get(len);
|
|
258
|
+
if (c !== undefined)
|
|
259
|
+
code[at] = runs[c][0] + len;
|
|
260
|
+
next.set(len, r);
|
|
261
|
+
}
|
|
262
|
+
const open = [];
|
|
263
|
+
for (let k = 0; k < n; k++) {
|
|
264
|
+
const c = s[k];
|
|
265
|
+
if (c === '\\') {
|
|
266
|
+
k++;
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
if (c === '`') {
|
|
270
|
+
if (code[k] > 0) {
|
|
271
|
+
k = code[k] - 1;
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
while (s[k + 1] === '`')
|
|
275
|
+
k++;
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
if (c === '[')
|
|
279
|
+
open.push(k);
|
|
280
|
+
else if (c === ']' && open.length)
|
|
281
|
+
bracket[open.pop()] = k;
|
|
282
|
+
}
|
|
283
|
+
if (marksOf.size > 512)
|
|
284
|
+
marksOf.clear();
|
|
285
|
+
const m = { code, bracket };
|
|
286
|
+
marksOf.set(s, m);
|
|
287
|
+
return m;
|
|
288
|
+
}
|
|
289
|
+
/** The end of a bracketed link text starting at `i` ("[" at i), nested brackets and code spans honoured; -1 for none. */
|
|
290
|
+
const closeBracket = (s, i) => marks(s).bracket[i];
|
|
291
|
+
/** Where a code span opened by the backtick run at `i` ends (after its closing run), or -1. */
|
|
292
|
+
const codeSpanEnd = (s, i) => marks(s).code[i];
|
|
293
|
+
/** An inline destination "(href "title")" starting at `i` ("(" at i): its parts and where it ends; null when malformed. */
|
|
294
|
+
function destination(s, i) {
|
|
295
|
+
let k = i + 1;
|
|
296
|
+
while (k < s.length && /[ \t\n]/.test(s[k]))
|
|
297
|
+
k++;
|
|
298
|
+
let href = '';
|
|
299
|
+
if (s[k] === '<') {
|
|
300
|
+
const e = s.indexOf('>', k);
|
|
301
|
+
if (e < 0 || s.slice(k, e).includes('\n'))
|
|
302
|
+
return null;
|
|
303
|
+
href = s.slice(k + 1, e);
|
|
304
|
+
k = e + 1;
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
let depth = 0;
|
|
308
|
+
const st = k;
|
|
309
|
+
while (k < s.length && !/[ \t\n]/.test(s[k])) {
|
|
310
|
+
if (s[k] === '\\' && PUNCT.test(s[k + 1] ?? '')) {
|
|
311
|
+
k += 2;
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
if (s[k] === '(')
|
|
315
|
+
depth++;
|
|
316
|
+
else if (s[k] === ')') {
|
|
317
|
+
if (!depth)
|
|
318
|
+
break;
|
|
319
|
+
depth--;
|
|
320
|
+
}
|
|
321
|
+
k++;
|
|
322
|
+
}
|
|
323
|
+
href = s.slice(st, k);
|
|
324
|
+
}
|
|
325
|
+
while (k < s.length && /[ \t\n]/.test(s[k]))
|
|
326
|
+
k++;
|
|
327
|
+
let title = '';
|
|
328
|
+
const q = s[k];
|
|
329
|
+
if (q === '"' || q === "'" || q === '(') {
|
|
330
|
+
const close = q === '(' ? ')' : q;
|
|
331
|
+
const e = s.indexOf(close, k + 1);
|
|
332
|
+
if (e < 0)
|
|
333
|
+
return null;
|
|
334
|
+
title = s.slice(k + 1, e);
|
|
335
|
+
k = e + 1;
|
|
336
|
+
while (k < s.length && /[ \t\n]/.test(s[k]))
|
|
337
|
+
k++;
|
|
338
|
+
}
|
|
339
|
+
if (s[k] !== ')')
|
|
340
|
+
return null;
|
|
341
|
+
return { href: unescape(href), title: unescape(title), end: k + 1 };
|
|
342
|
+
}
|
|
343
|
+
const unescape = (s) => s.replace(/\\([!-/:-@[-`{-~])/g, '$1');
|
|
344
|
+
/** The plain text of inline markdown (an image's alt, a link's text for its callback). */
|
|
345
|
+
const plain = (s) => unescape(s.replace(/!?\[([^\]]*)\]\([^)]*\)/g, '$1').replace(/[*_~`]+/g, '')).trim();
|
|
346
|
+
function linkHtml(l, inner, c, alone = false) {
|
|
347
|
+
const custom = c.o.link?.(l, inner, { alone });
|
|
348
|
+
if (typeof custom === 'string')
|
|
349
|
+
return custom;
|
|
350
|
+
if (custom) {
|
|
351
|
+
// the block follows the paragraph (once); in a table only the link stays
|
|
352
|
+
if (custom.block && c.blocks && !c.blocks.includes(custom.block))
|
|
353
|
+
c.blocks.push(custom.block);
|
|
354
|
+
return custom.inline;
|
|
355
|
+
}
|
|
356
|
+
const title = l.title ? ` title="${escHtml(l.title)}"` : '';
|
|
357
|
+
if (l.image)
|
|
358
|
+
return safeHref(l.href) && !/^(https?:|mailto:)/i.test(l.href) ? `<img src="${escHtml(l.href)}" alt="${escHtml(l.text)}"${title} loading="lazy">` : escHtml(l.text || l.href);
|
|
359
|
+
if (!safeHref(l.href))
|
|
360
|
+
return inner;
|
|
361
|
+
if (l.href.startsWith('#'))
|
|
362
|
+
return `<a href="#${escHtml((c.o.idPrefix ?? '') + slug(decodeSafe(l.href.slice(1))))}"${title}>${inner}</a>`;
|
|
363
|
+
const ext = /^(https?:|mailto:)/i.test(l.href);
|
|
364
|
+
return `<a href="${escHtml(l.href)}"${title}${ext ? ' target="_blank" rel="noopener noreferrer"' : ''}>${inner}</a>`;
|
|
365
|
+
}
|
|
366
|
+
const decodeSafe = (s) => { try {
|
|
367
|
+
return decodeURIComponent(s);
|
|
368
|
+
}
|
|
369
|
+
catch {
|
|
370
|
+
return s;
|
|
371
|
+
} };
|
|
372
|
+
function inline(s, c, depth = 0) {
|
|
373
|
+
if (depth > 32)
|
|
374
|
+
return escHtml(s);
|
|
375
|
+
let out = '';
|
|
376
|
+
let i = 0;
|
|
377
|
+
/** the link from `from` to `to` is all the paragraph holds */
|
|
378
|
+
const alone = (from, to) => !!c.para && depth === 0 && !c.inLink && !s.slice(0, from).trim() && !s.slice(to).trim();
|
|
379
|
+
// a closing run not found from some point is not found from any later point either: a paragraph full of lone "*"
|
|
380
|
+
// stays linear
|
|
381
|
+
const none = new Map();
|
|
382
|
+
const closerAt = (from, ch, size) => {
|
|
383
|
+
const k = ch + size;
|
|
384
|
+
if ((none.get(k) ?? Infinity) <= from)
|
|
385
|
+
return -1;
|
|
386
|
+
const e = closer(s, from, ch, size);
|
|
387
|
+
if (e < 0)
|
|
388
|
+
none.set(k, Math.min(none.get(k) ?? Infinity, from));
|
|
389
|
+
return e;
|
|
390
|
+
};
|
|
391
|
+
while (i < s.length) {
|
|
392
|
+
const ch = s[i];
|
|
393
|
+
// backslash escapes, and a hard break at the end of a line
|
|
394
|
+
if (ch === '\\') {
|
|
395
|
+
if (s[i + 1] === '\n') {
|
|
396
|
+
out += '<br>\n';
|
|
397
|
+
i += 2;
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
400
|
+
if (PUNCT.test(s[i + 1] ?? '')) {
|
|
401
|
+
out += escHtml(s[i + 1]);
|
|
402
|
+
i += 2;
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
out += '\\';
|
|
406
|
+
i++;
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
409
|
+
if (ch === '\n') {
|
|
410
|
+
out = / +$/.test(out) ? out.replace(/ +$/, '') + '<br>\n' : out.replace(/ +$/, '') + '\n';
|
|
411
|
+
i++;
|
|
412
|
+
while (s[i] === ' ')
|
|
413
|
+
i++;
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
if (ch === '`') {
|
|
417
|
+
const e = codeSpanEnd(s, i);
|
|
418
|
+
if (e > 0) {
|
|
419
|
+
let run = 0;
|
|
420
|
+
while (s[i + run] === '`')
|
|
421
|
+
run++;
|
|
422
|
+
let code = s.slice(i + run, e - run).replace(/\n/g, ' ');
|
|
423
|
+
if (/^ .*[^ ].* $/.test(code))
|
|
424
|
+
code = code.slice(1, -1);
|
|
425
|
+
out += `<code>${escHtml(code)}</code>`;
|
|
426
|
+
i = e;
|
|
427
|
+
continue;
|
|
428
|
+
}
|
|
429
|
+
let run = 0;
|
|
430
|
+
while (s[i + run] === '`')
|
|
431
|
+
run++;
|
|
432
|
+
out += s.slice(i, i + run);
|
|
433
|
+
i += run;
|
|
434
|
+
continue;
|
|
435
|
+
}
|
|
436
|
+
// images and links: inline "(dest)", full "[ref]", collapsed "[]" or shortcut references
|
|
437
|
+
if ((ch === '!' && s[i + 1] === '[') || (ch === '[' && !c.inLink)) {
|
|
438
|
+
const image = ch === '!';
|
|
439
|
+
const open = image ? i + 1 : i;
|
|
440
|
+
const close = closeBracket(s, open);
|
|
441
|
+
if (close > 0) {
|
|
442
|
+
const raw = s.slice(open + 1, close);
|
|
443
|
+
let target = null;
|
|
444
|
+
if (s[close + 1] === '(')
|
|
445
|
+
target = destination(s, close + 1);
|
|
446
|
+
if (!target) {
|
|
447
|
+
let label = raw, end = close + 1;
|
|
448
|
+
const full = /^\[([^\]]*)\]/.exec(s.slice(close + 1));
|
|
449
|
+
if (full) {
|
|
450
|
+
if (full[1].trim())
|
|
451
|
+
label = full[1];
|
|
452
|
+
end = close + 1 + full[0].length;
|
|
453
|
+
}
|
|
454
|
+
const ref = c.refs.get(label.trim().toLowerCase().replace(/\s+/g, ' '));
|
|
455
|
+
if (ref)
|
|
456
|
+
target = { href: ref.href, title: ref.title, end };
|
|
457
|
+
}
|
|
458
|
+
if (target) {
|
|
459
|
+
const l = { href: target.href, text: plain(raw), title: target.title, image };
|
|
460
|
+
const inner = image ? escHtml(l.text) : inline(raw, { ...c, inLink: true }, depth + 1);
|
|
461
|
+
out += linkHtml(l, inner, c, alone(i, target.end));
|
|
462
|
+
i = target.end;
|
|
463
|
+
continue;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
out += escHtml(ch);
|
|
467
|
+
i++;
|
|
468
|
+
continue;
|
|
469
|
+
}
|
|
470
|
+
// autolinks <https://…>, and bare http(s) URLs
|
|
471
|
+
if (ch === '<') {
|
|
472
|
+
const m = /^<((?:https?|mailto):[^\s<>]*)>/i.exec(s.slice(i));
|
|
473
|
+
if (m && !c.inLink) {
|
|
474
|
+
out += linkHtml({ href: m[1], text: m[1], title: '', image: false }, escHtml(m[1]), c, alone(i, i + m[0].length));
|
|
475
|
+
i += m[0].length;
|
|
476
|
+
continue;
|
|
477
|
+
}
|
|
478
|
+
out += '<';
|
|
479
|
+
i++;
|
|
480
|
+
continue;
|
|
481
|
+
}
|
|
482
|
+
if ((ch === 'h' || ch === 'H') && !c.inLink && !isAlnum(s[i - 1]) && /^https?:\/\//i.test(s.slice(i, i + 8))) {
|
|
483
|
+
let url = /^https?:\/\/[^\s<>"]+/i.exec(s.slice(i))[0];
|
|
484
|
+
// trailing punctuation ends the sentence, not the URL; so does a ")" without its "("
|
|
485
|
+
while (/[.,:;!?'"*_~]$/.test(url))
|
|
486
|
+
url = url.slice(0, -1);
|
|
487
|
+
while (url.endsWith(')') && (url.match(/\(/g)?.length ?? 0) < (url.match(/\)/g)?.length ?? 0))
|
|
488
|
+
url = url.slice(0, -1);
|
|
489
|
+
out += linkHtml({ href: url, text: url, title: '', image: false }, escHtml(url), c, alone(i, i + url.length));
|
|
490
|
+
i += url.length;
|
|
491
|
+
continue;
|
|
492
|
+
}
|
|
493
|
+
// emphasis, strong and strikethrough
|
|
494
|
+
if (ch === '*' || ch === '_' || ch === '~') {
|
|
495
|
+
let run = 0;
|
|
496
|
+
while (s[i + run] === ch)
|
|
497
|
+
run++;
|
|
498
|
+
const before = s[i - 1], after = s[i + run];
|
|
499
|
+
const leftFlank = !isSpace(after) && (!isPunct(after) || isSpace(before) || isPunct(before));
|
|
500
|
+
const canOpen = leftFlank && (ch !== '_' || !isAlnum(before)) && (ch !== '~' || run === 2);
|
|
501
|
+
if (canOpen) {
|
|
502
|
+
const sizes = ch === '~' ? [2] : run >= 3 ? [3, 2, 1] : run === 2 ? [2, 1] : [1];
|
|
503
|
+
let done = false;
|
|
504
|
+
for (const size of sizes) {
|
|
505
|
+
const from = i + run;
|
|
506
|
+
const end = closerAt(from, ch, size);
|
|
507
|
+
if (end < 0)
|
|
508
|
+
continue;
|
|
509
|
+
const lead = s.slice(i, i + run - size);
|
|
510
|
+
const inner = inline(s.slice(from, end), c, depth + 1);
|
|
511
|
+
const wrap = ch === '~' ? `<del>${inner}</del>` : size === 3 ? `<em><strong>${inner}</strong></em>` : size === 2 ? `<strong>${inner}</strong>` : `<em>${inner}</em>`;
|
|
512
|
+
out += escHtml(lead) + wrap;
|
|
513
|
+
i = end + size;
|
|
514
|
+
done = true;
|
|
515
|
+
break;
|
|
516
|
+
}
|
|
517
|
+
if (done)
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
out += escHtml(s.slice(i, i + run));
|
|
521
|
+
i += run;
|
|
522
|
+
continue;
|
|
523
|
+
}
|
|
524
|
+
// plain text up to the next character that may start something
|
|
525
|
+
let k = i + 1;
|
|
526
|
+
while (k < s.length && !/[\\\n`!\[<*_~hH]/.test(s[k]))
|
|
527
|
+
k++;
|
|
528
|
+
out += escHtml(s.slice(i, k));
|
|
529
|
+
i = k;
|
|
530
|
+
}
|
|
531
|
+
return out;
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* The start of the run that closes emphasis of `size` characters `ch` opened before `from`: a run of at least `size`
|
|
535
|
+
* that can close (no space before it; for "_" no letter after it), skipping code spans, escapes and links; -1 for none.
|
|
536
|
+
*/
|
|
537
|
+
function closer(s, from, ch, size) {
|
|
538
|
+
for (let j = from; j < s.length; j++) {
|
|
539
|
+
const c = s[j];
|
|
540
|
+
if (c === '\\') {
|
|
541
|
+
j++;
|
|
542
|
+
continue;
|
|
543
|
+
}
|
|
544
|
+
if (c === '`') {
|
|
545
|
+
const e = codeSpanEnd(s, j);
|
|
546
|
+
if (e > 0) {
|
|
547
|
+
j = e - 1;
|
|
548
|
+
continue;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
if (c === '[') {
|
|
552
|
+
const e = closeBracket(s, j);
|
|
553
|
+
if (e > 0) {
|
|
554
|
+
const d = s[e + 1] === '(' ? destination(s, e + 1) : null;
|
|
555
|
+
j = d ? d.end - 1 : e;
|
|
556
|
+
continue;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
if (c !== ch)
|
|
560
|
+
continue;
|
|
561
|
+
let run = 0;
|
|
562
|
+
while (s[j + run] === ch)
|
|
563
|
+
run++;
|
|
564
|
+
const before = s[j - 1], after = s[j + run];
|
|
565
|
+
const rightFlank = !isSpace(before) && (!isPunct(before) || isSpace(after) || isPunct(after));
|
|
566
|
+
if (j > from && run >= size && rightFlank && (ch !== '_' || !isAlnum(after)) && (ch !== '~' || run === 2))
|
|
567
|
+
return j + (run - size);
|
|
568
|
+
j += run - 1;
|
|
569
|
+
}
|
|
570
|
+
return -1;
|
|
571
|
+
}
|
|
572
|
+
// ---------------------------------------------------------------------------------------------------------- render
|
|
573
|
+
/** Blocks the links of a paragraph or heading show below it: cards and pictures side by side, players full width. */
|
|
574
|
+
const below = (blocks) => (blocks.length ? `<div class="lblocks">${blocks.join('')}</div>` : '');
|
|
575
|
+
/** Table cells this short keep to one line ("0.92 m", "lod0 tris"): the table scrolls before they break. */
|
|
576
|
+
const SHORT_CELL = 12;
|
|
577
|
+
function renderBlocks(blocks, c, tight = false) {
|
|
578
|
+
const used = new Map();
|
|
579
|
+
const id = (text) => {
|
|
580
|
+
const base = slug(text) || 'section';
|
|
581
|
+
const n = used.get(base) ?? 0;
|
|
582
|
+
used.set(base, n + 1);
|
|
583
|
+
return (c.o.idPrefix ?? '') + (n ? `${base}-${n}` : base);
|
|
584
|
+
};
|
|
585
|
+
const cell = (tag, text, style) => `<${tag}${style}${plain(text).length <= SHORT_CELL ? ' class="n"' : ''}>${inline(text, { ...c, para: false, blocks: undefined })}</${tag}>`;
|
|
586
|
+
return blocks.map((b) => {
|
|
587
|
+
switch (b.t) {
|
|
588
|
+
case 'p': {
|
|
589
|
+
const after = [];
|
|
590
|
+
const html = inline(b.text, { ...c, para: true, blocks: after });
|
|
591
|
+
const text = html.trim() ? (tight ? html : `<p>${html}</p>`) : '';
|
|
592
|
+
return text + below(after);
|
|
593
|
+
}
|
|
594
|
+
case 'h': {
|
|
595
|
+
const after = [];
|
|
596
|
+
const html = inline(b.text, { ...c, para: false, blocks: after });
|
|
597
|
+
return `<h${b.level} id="${escHtml(id(html))}">${html}</h${b.level}>${below(after)}`;
|
|
598
|
+
}
|
|
599
|
+
case 'code': return `<pre><code${b.info ? ` class="language-${escHtml(b.info.replace(/[^\w+-]/g, ''))}"` : ''}>${escHtml(b.text)}</code></pre>`;
|
|
600
|
+
case 'hr': return '<hr>';
|
|
601
|
+
case 'quote': return `<blockquote>${renderBlocks(b.blocks, c)}</blockquote>`;
|
|
602
|
+
case 'list': {
|
|
603
|
+
const tag = b.ordered ? 'ol' : 'ul';
|
|
604
|
+
const items = b.items.map((it) => {
|
|
605
|
+
const box = it.task === null ? '' : `<input type="checkbox" disabled${it.task ? ' checked' : ''}> `;
|
|
606
|
+
return `<li${it.task === null ? '' : ' class="task"'}>${box}${renderBlocks(it.blocks, c, !b.loose)}</li>`;
|
|
607
|
+
}).join('');
|
|
608
|
+
return `<${tag}${b.ordered && b.start !== 1 ? ` start="${b.start}"` : ''}>${items}</${tag}>`;
|
|
609
|
+
}
|
|
610
|
+
case 'table': {
|
|
611
|
+
const al = (k) => (b.align[k] ? ` style="text-align:${b.align[k]}"` : '');
|
|
612
|
+
return `<div class="table"><table><thead><tr>${b.head.map((h, k) => cell('th', h, al(k))).join('')}</tr></thead>`
|
|
613
|
+
+ `<tbody>${b.rows.map((r) => `<tr>${r.map((d, k) => cell('td', d, al(k))).join('')}</tr>`).join('')}</tbody></table></div>`;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}).join('\n');
|
|
617
|
+
}
|
|
618
|
+
const toLines = (src) => src.replace(/^/, '').replace(/\r\n?/g, '\n').replace(/\t/g, ' ').split('\n');
|
|
619
|
+
/**
|
|
620
|
+
* Front matter: a first line "---", key: value lines (lists below a key as "- item" lines), and a closing "---" or
|
|
621
|
+
* "..." (as GitHub and Obsidian read it); the properties in order and where the document starts after it, or null.
|
|
622
|
+
*/
|
|
623
|
+
export function frontMatter(lines) {
|
|
624
|
+
if (!/^---[ \t]*$/.test(lines[0] ?? ''))
|
|
625
|
+
return null;
|
|
626
|
+
const end = lines.findIndex((l, i) => i > 0 && /^(---|\.\.\.)[ \t]*$/.test(l));
|
|
627
|
+
if (end < 0 || end > 400)
|
|
628
|
+
return null;
|
|
629
|
+
const body = lines.slice(1, end);
|
|
630
|
+
const first = body.find((l) => l.trim() && !l.trim().startsWith('#'));
|
|
631
|
+
if (first !== undefined && !/^[A-Za-z0-9_][\w .-]*:([ \t]|$)/.test(first))
|
|
632
|
+
return null;
|
|
633
|
+
const props = [];
|
|
634
|
+
const value = (v) => {
|
|
635
|
+
v = v.trim().replace(/[ \t]+#.*$/, '');
|
|
636
|
+
const q = /^'(.*)'$/.exec(v) ?? /^"(.*)"$/.exec(v);
|
|
637
|
+
if (q)
|
|
638
|
+
return v.startsWith("'") ? q[1].replace(/''/g, "'") : q[1].replace(/\\"/g, '"');
|
|
639
|
+
const list = /^\[(.*)\]$/.exec(v);
|
|
640
|
+
return list ? list[1].split(',').map((x) => value(x)).filter(Boolean).join(', ') : v;
|
|
641
|
+
};
|
|
642
|
+
for (const l of body) {
|
|
643
|
+
if (!l.trim() || l.trim().startsWith('#'))
|
|
644
|
+
continue;
|
|
645
|
+
const kv = /^([A-Za-z0-9_][\w .-]*):(?:[ \t]+(.*))?$/.exec(l);
|
|
646
|
+
if (kv) {
|
|
647
|
+
props.push([kv[1].trim(), value(kv[2] ?? '')]);
|
|
648
|
+
continue;
|
|
649
|
+
}
|
|
650
|
+
// a list item or a continuation below the last key
|
|
651
|
+
const last = props[props.length - 1];
|
|
652
|
+
if (!last)
|
|
653
|
+
continue;
|
|
654
|
+
const item = value(l.trim().replace(/^-[ \t]*/, ''));
|
|
655
|
+
if (item)
|
|
656
|
+
last[1] = last[1] ? `${last[1]}, ${item}` : item;
|
|
657
|
+
}
|
|
658
|
+
return { props, next: end + 1 };
|
|
659
|
+
}
|
|
660
|
+
/** The HTML of a markdown document: escaped throughout, links and images through `o.link`. */
|
|
661
|
+
export function renderMarkdown(src, o = {}) {
|
|
662
|
+
const refs = new Map();
|
|
663
|
+
let lines = toLines(src);
|
|
664
|
+
const fm = frontMatter(lines);
|
|
665
|
+
if (fm)
|
|
666
|
+
lines = lines.slice(fm.next);
|
|
667
|
+
const blocks = parseBlocks(lines, refs);
|
|
668
|
+
const shown = fm?.props.filter(([, v]) => v) ?? [];
|
|
669
|
+
const props = shown.length ? `<dl class="props">${shown.map(([k, v]) => `<div><dt>${escHtml(k)}</dt><dd>${escHtml(v)}</dd></div>`).join('')}</dl>\n` : '';
|
|
670
|
+
return props + renderBlocks(blocks, { refs, o, inLink: false });
|
|
671
|
+
}
|
|
672
|
+
/** Every link and image of a markdown document, in order (what renderMarkdown hands its callback). */
|
|
673
|
+
export function markdownLinks(src) {
|
|
674
|
+
const out = [];
|
|
675
|
+
renderMarkdown(src, { link: (l) => { out.push(l); return ''; } });
|
|
676
|
+
return out;
|
|
677
|
+
}
|