@dogsbay/format-obsidian 0.2.0-beta.17 → 0.2.0-beta.170
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/capabilities.d.ts +25 -0
- package/dist/capabilities.js +171 -0
- package/dist/cli.d.ts +0 -1
- package/dist/cli.js +162 -62
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/project.d.ts +49 -3
- package/dist/project.js +561 -44
- package/dist/serialize.d.ts +19 -1
- package/dist/serialize.js +734 -134
- package/package.json +9 -5
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/project.d.ts.map +0 -1
- package/dist/project.js.map +0 -1
- package/dist/serialize.d.ts.map +0 -1
- package/dist/serialize.js.map +0 -1
package/dist/serialize.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { walkInline, applyTextFlags, renderLeaf, prefixLines, pickCodeFence, stripHtml, inlineHtmlToMarkdown, escapeMarkdownText, isSafeHref, codeSpan, markdownDestination, markdownTitle, markdownLatex, footnoteLabel, mapOutsideCodeSpans, neutralizeMarkdown, renderAroundGroup, taskMarker, } from "@dogsbay/serialize-core";
|
|
1
2
|
/**
|
|
2
3
|
* Transform an href for Obsidian vault paths.
|
|
3
4
|
*
|
|
@@ -7,13 +8,68 @@
|
|
|
7
8
|
* https://example.com → https://example.com (unchanged)
|
|
8
9
|
* #local-anchor → #local-anchor (unchanged)
|
|
9
10
|
*/
|
|
11
|
+
/**
|
|
12
|
+
* `/_assets/x.png` → `_assets/x.png`.
|
|
13
|
+
*
|
|
14
|
+
* Importers rewrite media refs to the `/_assets/…` mount convention, which is
|
|
15
|
+
* right for a served site and wrong in a vault: Obsidian resolves a link
|
|
16
|
+
* against the vault, so a leading slash finds nothing — even though the
|
|
17
|
+
* exporter copied the file to `_assets/x.png`. External and data URIs are
|
|
18
|
+
* left alone.
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* Make asset references inside RAW HTML resolvable from the vault.
|
|
22
|
+
*
|
|
23
|
+
* A served site roots them (`/_assets/images/x.png`); a vault has no site
|
|
24
|
+
* root, so the same path resolves nowhere. `vaultAssetPath` already handles
|
|
25
|
+
* this for `image` inline nodes, but a complex AsciiDoc table is emitted as
|
|
26
|
+
* raw HTML and its cells carry their own `<img src>` — which never reaches
|
|
27
|
+
* that renderer. Five images in an OpenShift vault were dead for exactly this
|
|
28
|
+
* reason while the identical file resolved fine one paragraph earlier.
|
|
29
|
+
*/
|
|
30
|
+
function vaultifyHtmlAssets(html) {
|
|
31
|
+
if (!html.includes("/_assets/"))
|
|
32
|
+
return html;
|
|
33
|
+
return html
|
|
34
|
+
.replace(/(\ssrc=")\/+(_assets\/)/g, "$1$2")
|
|
35
|
+
.replace(/(\shref=")\/+(_assets\/)/g, "$1$2");
|
|
36
|
+
}
|
|
37
|
+
function vaultAssetPath(src) {
|
|
38
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(src))
|
|
39
|
+
return src;
|
|
40
|
+
return src.replace(/^\/+/, "");
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Sanitize a slug into a valid Obsidian BLOCK IDENTIFIER.
|
|
44
|
+
*
|
|
45
|
+
* Obsidian block ids accept only letters, numbers and dashes. Anything else
|
|
46
|
+
* means the `^id` is not parsed as a block id at all — it renders as literal
|
|
47
|
+
* text in BOTH edit and reading view, and any `#^id` link pointing at it is
|
|
48
|
+
* dead.
|
|
49
|
+
*
|
|
50
|
+
* This is not a corner case: AsciiDoc ids join a module and its context with
|
|
51
|
+
* an underscore (`additional-resources_welcome-index`), so a real OpenShift
|
|
52
|
+
* vault had 21,509 unusable anchors across 82% of its notes, and 2,333 of its
|
|
53
|
+
* cross-references (21%) resolved to nothing. The visible tell is that a
|
|
54
|
+
* dash-only id like `^welcome-index` superscripts and hides correctly while
|
|
55
|
+
* its underscore sibling does not.
|
|
56
|
+
*
|
|
57
|
+
* MUST be applied to the anchor and to every link that targets it, or the two
|
|
58
|
+
* stop agreeing — which is worse than leaving both broken.
|
|
59
|
+
*/
|
|
60
|
+
export function obsidianBlockId(slug) {
|
|
61
|
+
return slug
|
|
62
|
+
.replace(/[^A-Za-z0-9-]+/g, "-")
|
|
63
|
+
.replace(/-{2,}/g, "-")
|
|
64
|
+
.replace(/^-+|-+$/g, "");
|
|
65
|
+
}
|
|
10
66
|
function obsidianHref(href) {
|
|
11
67
|
// External URLs — leave as-is
|
|
12
68
|
if (href.startsWith("http://") || href.startsWith("https://"))
|
|
13
69
|
return href;
|
|
14
70
|
// Local anchors — rewrite to block ID reference
|
|
15
71
|
if (href.startsWith("#"))
|
|
16
|
-
return `#^${href.slice(1)}`;
|
|
72
|
+
return `#^${obsidianBlockId(href.slice(1))}`;
|
|
17
73
|
// Strip leading /
|
|
18
74
|
let result = href.replace(/^\//, "");
|
|
19
75
|
// Strip trailing /
|
|
@@ -21,13 +77,43 @@ function obsidianHref(href) {
|
|
|
21
77
|
// Normalize /#anchor to #anchor (strip / before #)
|
|
22
78
|
result = result.replace(/\/#/, "#");
|
|
23
79
|
// Rewrite #slug anchors to #^slug block ID references
|
|
24
|
-
result = result.replace(/#(.+)$/,
|
|
80
|
+
result = result.replace(/#(.+)$/, (_m, frag) => `#^${obsidianBlockId(frag)}`);
|
|
25
81
|
return result;
|
|
26
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* Block ids already emitted for the CURRENT page.
|
|
85
|
+
*
|
|
86
|
+
* Reset per page in `treeToObsidian` (project.ts calls it exactly once per
|
|
87
|
+
* page). Obsidian indexes a block id per note, so two headings sharing one id
|
|
88
|
+
* make every `#^id` link resolve to whichever comes first — silently.
|
|
89
|
+
*
|
|
90
|
+
* Measured on OpenShift 4.22: 1,855 duplicate ids were ALREADY present in the
|
|
91
|
+
* AsciiDoc source, and sanitizing added only 2 more. So this mostly cleans up
|
|
92
|
+
* a pre-existing source problem rather than one we introduced. Links keep
|
|
93
|
+
* targeting the un-suffixed id and therefore still land on the first heading —
|
|
94
|
+
* the same behaviour as before, but without Obsidian seeing a duplicate.
|
|
95
|
+
*/
|
|
96
|
+
const usedBlockIds = new Set();
|
|
97
|
+
function uniqueBlockId(id) {
|
|
98
|
+
if (!id)
|
|
99
|
+
return id;
|
|
100
|
+
if (!usedBlockIds.has(id)) {
|
|
101
|
+
usedBlockIds.add(id);
|
|
102
|
+
return id;
|
|
103
|
+
}
|
|
104
|
+
for (let n = 1;; n++) {
|
|
105
|
+
const candidate = `${id}-${n}`;
|
|
106
|
+
if (!usedBlockIds.has(candidate)) {
|
|
107
|
+
usedBlockIds.add(candidate);
|
|
108
|
+
return candidate;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
27
112
|
/**
|
|
28
113
|
* Convert TreeNode[] to Obsidian-flavored Markdown.
|
|
29
114
|
*/
|
|
30
115
|
export function treeToObsidian(nodes) {
|
|
116
|
+
usedBlockIds.clear();
|
|
31
117
|
return nodes.map(nodeToObsidian).filter(Boolean).join("\n\n");
|
|
32
118
|
}
|
|
33
119
|
// ── Block dispatcher ────────────────────────────────────
|
|
@@ -40,7 +126,7 @@ function nodeToObsidian(node) {
|
|
|
40
126
|
case "heading":
|
|
41
127
|
return headingToObsidian(node);
|
|
42
128
|
case "paragraph":
|
|
43
|
-
return
|
|
129
|
+
return leafContent(node);
|
|
44
130
|
case "prose":
|
|
45
131
|
return proseToObsidian(node);
|
|
46
132
|
case "callout":
|
|
@@ -48,7 +134,7 @@ function nodeToObsidian(node) {
|
|
|
48
134
|
case "details":
|
|
49
135
|
return detailsToObsidian(node);
|
|
50
136
|
case "tabs":
|
|
51
|
-
return
|
|
137
|
+
return renderAroundGroup(node, "tab", tabsToObsidian, nodeToObsidian, "\n\n");
|
|
52
138
|
case "code":
|
|
53
139
|
return codeToObsidian(node);
|
|
54
140
|
case "diagram":
|
|
@@ -68,9 +154,9 @@ function nodeToObsidian(node) {
|
|
|
68
154
|
case "deflist":
|
|
69
155
|
return deflistToObsidian(node);
|
|
70
156
|
case "dt":
|
|
71
|
-
return `**${
|
|
157
|
+
return `**${leafContent(node)}**`;
|
|
72
158
|
case "dd":
|
|
73
|
-
return `: ${
|
|
159
|
+
return `: ${leafContent(node)}`;
|
|
74
160
|
case "steps":
|
|
75
161
|
return stepsToObsidian(node);
|
|
76
162
|
case "step":
|
|
@@ -79,21 +165,43 @@ function nodeToObsidian(node) {
|
|
|
79
165
|
return childrenToObsidian(node);
|
|
80
166
|
case "annotation-list":
|
|
81
167
|
return childrenToObsidian(node);
|
|
82
|
-
case "link-button":
|
|
83
|
-
|
|
84
|
-
const text = childrenToObsidian(node) || href;
|
|
85
|
-
return `[${text}](${href})`;
|
|
86
|
-
}
|
|
168
|
+
case "link-button":
|
|
169
|
+
return linkButtonToObsidian(node);
|
|
87
170
|
case "hr":
|
|
171
|
+
// The MDX importers (Starlight, Docusaurus) emit `thematic-break` for a
|
|
172
|
+
// `---`; format-dogsbay-md emits `hr`. With no case, the default path
|
|
173
|
+
// returned "" for the MDX shape and every rule vanished from the vault.
|
|
174
|
+
case "thematic-break":
|
|
88
175
|
return "---";
|
|
89
176
|
case "html":
|
|
90
|
-
|
|
177
|
+
// A lone closing tag of a COMPONENT (`</Preview>`, `</FileTree>`): the
|
|
178
|
+
// MDX importer consumed the opener as JSX, but a closer that follows a
|
|
179
|
+
// blank line is an html_block to markdown-it and arrives here as raw
|
|
180
|
+
// HTML. Obsidian shows it as literal text — hundreds of lines on
|
|
181
|
+
// Starlight's docs. An HTML element's closer (`</div>`) is kept: it may
|
|
182
|
+
// pair with an opener passed through earlier.
|
|
183
|
+
if (isOrphanComponentCloser(node.html ?? ""))
|
|
184
|
+
return "";
|
|
185
|
+
return vaultifyHtmlAssets(node.html ?? "");
|
|
91
186
|
case "html-container":
|
|
187
|
+
// Parsed from raw HTML (a one-line `<details>` body), the content is in
|
|
188
|
+
// `html` with no children — rendering only children dropped it
|
|
189
|
+
if (!node.children?.length && node.html)
|
|
190
|
+
return vaultifyHtmlAssets(node.html);
|
|
92
191
|
return childrenToObsidian(node);
|
|
192
|
+
case "link-card":
|
|
193
|
+
// Same shape as a card with its one-paragraph body in
|
|
194
|
+
// `props.description`; `cardToObsidian` reads that itself.
|
|
195
|
+
return cardToObsidian(node);
|
|
196
|
+
case "accordion":
|
|
197
|
+
return (node.children ?? []).map(nodeToObsidian).filter(Boolean).join("\n\n");
|
|
198
|
+
case "accordion-item":
|
|
199
|
+
// Each item is a collapsed fold titled with its label
|
|
200
|
+
return detailsToObsidian({ ...node, props: { title: node.props?.label ?? node.props?.title, open: node.props?.open } });
|
|
93
201
|
case "figure": {
|
|
94
202
|
const caption = node.props?.caption;
|
|
95
203
|
const inner = childrenToObsidian(node);
|
|
96
|
-
return caption ? `${inner}\n\n*${caption}*` : inner;
|
|
204
|
+
return caption ? `${inner}\n\n*${esc(oneLine(caption))}*` : inner;
|
|
97
205
|
}
|
|
98
206
|
case "card":
|
|
99
207
|
return cardToObsidian(node);
|
|
@@ -111,81 +219,192 @@ function nodeToObsidian(node) {
|
|
|
111
219
|
return apiParamsToObsidian(node);
|
|
112
220
|
case "api-source":
|
|
113
221
|
return apiSourceToObsidian(node);
|
|
222
|
+
case "endpoint":
|
|
223
|
+
return endpointToObsidian(node);
|
|
114
224
|
case "footnote-list":
|
|
115
225
|
return footnoteListToObsidian(node);
|
|
116
226
|
case "footnote-def":
|
|
117
227
|
return footnoteDefToObsidian(node);
|
|
228
|
+
case "related": {
|
|
229
|
+
// "Additional resources" blocks — the parser models the labeled
|
|
230
|
+
// link-list as a `related` node carrying its TITLE in props. The
|
|
231
|
+
// default fallthrough emitted only the children, so every such
|
|
232
|
+
// label vanished from the vault (issue #064: first-of-two blocks,
|
|
233
|
+
// the 8 per-section labels on oadp-api, nodes-nodes-jobs' heading —
|
|
234
|
+
// links floated with no context). A missing serializer case is a
|
|
235
|
+
// silent loss.
|
|
236
|
+
const title = (node.props?.title ?? "").trim();
|
|
237
|
+
const body = childrenToObsidian(node);
|
|
238
|
+
// No invented default label (format-astro omits it too — and an
|
|
239
|
+
// English fallback is wrong for localized corpora); escape emphasis
|
|
240
|
+
// chars so a title containing `*` can't break the bold wrapper.
|
|
241
|
+
if (!title)
|
|
242
|
+
return body;
|
|
243
|
+
return `**${esc(oneLine(title))}**\n\n${body}`;
|
|
244
|
+
}
|
|
118
245
|
default:
|
|
119
246
|
if (node.html)
|
|
120
|
-
return
|
|
247
|
+
return htmlToObsidian(node.html);
|
|
121
248
|
if (node.children)
|
|
122
249
|
return childrenToObsidian(node);
|
|
123
250
|
return "";
|
|
124
251
|
}
|
|
125
252
|
}
|
|
126
253
|
// ── Inline rendering ────────────────────────────────────
|
|
254
|
+
/**
|
|
255
|
+
* Obsidian's inline syntax, as emitters over serialize-core's shared walk.
|
|
256
|
+
*
|
|
257
|
+
* The traversal, the nesting recursion, and the bold/italic/strike flag order
|
|
258
|
+
* used to live here as a fourth copy of the same 11-variant switch. Only the
|
|
259
|
+
* leaf syntax below is Obsidian-specific; everything structural is now shared,
|
|
260
|
+
* so a variant added to `InlineNode` surfaces in one place instead of four.
|
|
261
|
+
*
|
|
262
|
+
* Emitters are exhaustive on purpose: an omission routes through the core's
|
|
263
|
+
* `onUnsupported` plain-text degradation, which is the right default for a new
|
|
264
|
+
* variant but the WRONG behaviour for one Obsidian genuinely supports.
|
|
265
|
+
*/
|
|
266
|
+
const OBSIDIAN_INLINE = {
|
|
267
|
+
text: (node) =>
|
|
268
|
+
// Escape tag-like `<` in prose: markdown-it decoded the engine's
|
|
269
|
+
// `<` entities into bare `<`, and Obsidian's reading view swallows
|
|
270
|
+
// `<cluster-name>` as an unknown HTML tag — the placeholder text
|
|
271
|
+
// silently disappears for the reader (issue #046). A backslash escape
|
|
272
|
+
// renders as the literal character.
|
|
273
|
+
// Every other Markdown marker too (`*x*`, `[x](…)`): text is literal.
|
|
274
|
+
applyTextFlags(esc(node.text), node, {
|
|
275
|
+
bold: (s) => `**${s}**`,
|
|
276
|
+
italic: (s) => `*${s}*`,
|
|
277
|
+
strike: (s) => `~~${s}~~`,
|
|
278
|
+
}),
|
|
279
|
+
link: (node, ctx) => {
|
|
280
|
+
if (!isSafeHref(node.href)) {
|
|
281
|
+
ctx.report("link", `unsafe link target dropped (${node.href.slice(0, 40)})`);
|
|
282
|
+
return ctx.recurse(node.children);
|
|
283
|
+
}
|
|
284
|
+
return `[${ctx.recurse(node.children)}](${markdownDestination(obsidianHref(node.href))}${markdownTitle(node.title)})`;
|
|
285
|
+
},
|
|
286
|
+
// A leading slash is not a vault path — see vaultAssetPath.
|
|
287
|
+
image: (node, ctx) => {
|
|
288
|
+
if (!isSafeHref(node.src, "image")) {
|
|
289
|
+
ctx.report("image", `unsafe image source dropped (${node.src.slice(0, 40)})`);
|
|
290
|
+
return esc(node.alt ?? "");
|
|
291
|
+
}
|
|
292
|
+
return `![${(node.alt ?? "").replace(/[[\]\\]/g, "\\$&")}](${markdownDestination(vaultAssetPath(node.src))})`;
|
|
293
|
+
},
|
|
294
|
+
code: (node) => codeSpan(node.text),
|
|
295
|
+
highlight: (node, ctx) => `==${ctx.recurse(node.children)}==`,
|
|
296
|
+
"footnote-ref": (node) => `[^${footnoteLabel(node.label)}]`,
|
|
297
|
+
kbd: (node) => node.keys.map((k) => `<kbd>${escapeHtmlChars(k)}</kbd>`).join("+"),
|
|
298
|
+
math: (node) => `$${markdownLatex(node.latex)}$`,
|
|
299
|
+
icon: (node) => `:${node.name.replace(/[^\w+-]/g, "")}:`,
|
|
300
|
+
"html-inline": (node) => node._composedRun
|
|
301
|
+
? (node.html ?? "")
|
|
302
|
+
: vaultifyHtmlAssets(node.html ?? ""),
|
|
303
|
+
break: () => " \n",
|
|
304
|
+
};
|
|
127
305
|
function inlineToObsidian(nodes) {
|
|
128
|
-
|
|
306
|
+
// `escape`: inline nodes without their own emitter fall back to plain text.
|
|
307
|
+
return walkInline(groupFormatRuns(nodes), { emitters: OBSIDIAN_INLINE, escape: esc });
|
|
129
308
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
309
|
+
/**
|
|
310
|
+
* Wrap CONSECUTIVE nodes sharing the same bold/italic/strike flags in one
|
|
311
|
+
* mark pair instead of per-node wrapping. The parser inherits strong/em
|
|
312
|
+
* flags onto code spans (`**`code` text**`, issue #045); per-node
|
|
313
|
+
* emission produced `` `code`** text** `` — the code span unbolded and
|
|
314
|
+
* `** text**` (space inside the opener) not even parsing as bold. Runs
|
|
315
|
+
* are emitted as a single html-inline-free group: flags stripped from the
|
|
316
|
+
* clones, marks around the whole run, run-edge spaces shifted OUTSIDE the
|
|
317
|
+
* marks (CommonMark flanking).
|
|
318
|
+
*/
|
|
319
|
+
function groupFormatRuns(nodes) {
|
|
320
|
+
const flagsOf = (n) => {
|
|
321
|
+
const f = n;
|
|
322
|
+
return `${f.bold ? "b" : ""}${f.italic ? "i" : ""}${f.strikethrough ? "s" : ""}`;
|
|
323
|
+
};
|
|
324
|
+
const flaggable = (n) => n.type === "text" || n.type === "code";
|
|
325
|
+
const out = [];
|
|
326
|
+
let i = 0;
|
|
327
|
+
while (i < nodes.length) {
|
|
328
|
+
const key = flagsOf(nodes[i]);
|
|
329
|
+
if (!key || !flaggable(nodes[i])) {
|
|
330
|
+
out.push(nodes[i]);
|
|
331
|
+
i++;
|
|
332
|
+
continue;
|
|
143
333
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
334
|
+
let j = i;
|
|
335
|
+
while (j < nodes.length && flaggable(nodes[j]) && flagsOf(nodes[j]) === key)
|
|
336
|
+
j++;
|
|
337
|
+
// A single flagged TEXT node keeps the proven per-node path (its
|
|
338
|
+
// escaping and flag order are pinned by the byte-parity suite); the
|
|
339
|
+
// synthetic group exists only for the broken shapes — a flagged code
|
|
340
|
+
// span, or a run split across several nodes.
|
|
341
|
+
if (j === i + 1 && nodes[i].type === "text") {
|
|
342
|
+
out.push(nodes[i]);
|
|
343
|
+
i = j;
|
|
344
|
+
continue;
|
|
148
345
|
}
|
|
149
|
-
|
|
150
|
-
const
|
|
151
|
-
|
|
346
|
+
const run = nodes.slice(i, j).map((n) => {
|
|
347
|
+
const clone = { ...n };
|
|
348
|
+
delete clone.bold;
|
|
349
|
+
delete clone.italic;
|
|
350
|
+
delete clone.strikethrough;
|
|
351
|
+
return clone;
|
|
352
|
+
});
|
|
353
|
+
let inner = walkInline(run, { emitters: OBSIDIAN_INLINE, escape: esc });
|
|
354
|
+
const lead = inner.match(/^\s*/)[0];
|
|
355
|
+
const tail = inner.match(/\s*$/)[0];
|
|
356
|
+
inner = inner.slice(lead.length, inner.length - tail.length || undefined);
|
|
357
|
+
if (inner) {
|
|
358
|
+
let open = "";
|
|
359
|
+
let close = "";
|
|
360
|
+
if (key.includes("s")) {
|
|
361
|
+
open += "~~";
|
|
362
|
+
close = "~~" + close;
|
|
363
|
+
}
|
|
364
|
+
if (key.includes("i")) {
|
|
365
|
+
open += "*";
|
|
366
|
+
close = "*" + close;
|
|
367
|
+
}
|
|
368
|
+
if (key.includes("b")) {
|
|
369
|
+
open += "**";
|
|
370
|
+
close = "**" + close;
|
|
371
|
+
}
|
|
372
|
+
// _composedRun: this is already-serialized MARKDOWN riding the
|
|
373
|
+
// html-inline pass-through, not raw HTML — the emitter must not run
|
|
374
|
+
// vaultifyHtmlAssets over it (it would rewrite `src="/_assets/…"`
|
|
375
|
+
// even inside a code span within the run).
|
|
376
|
+
out.push({ type: "html-inline", html: lead + open + inner + close + tail, _composedRun: true });
|
|
152
377
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
case "kbd":
|
|
160
|
-
return node.keys.map((k) => `<kbd>${k}</kbd>`).join("+");
|
|
161
|
-
case "math":
|
|
162
|
-
return `$${node.latex}$`;
|
|
163
|
-
case "icon":
|
|
164
|
-
return `:${node.name}:`;
|
|
165
|
-
case "html-inline":
|
|
166
|
-
return node.html;
|
|
167
|
-
case "break":
|
|
168
|
-
return " \n";
|
|
169
|
-
default:
|
|
170
|
-
return "";
|
|
378
|
+
else if (lead) {
|
|
379
|
+
// Whitespace-only run: lead and tail both matched the WHOLE string,
|
|
380
|
+
// so pushing lead+tail doubled the spaces. lead alone IS the run.
|
|
381
|
+
out.push({ type: "text", text: lead });
|
|
382
|
+
}
|
|
383
|
+
i = j;
|
|
171
384
|
}
|
|
385
|
+
return out;
|
|
172
386
|
}
|
|
173
387
|
// ── Prose (inline or HTML fallback) ─────────────────────
|
|
174
388
|
function proseToObsidian(node) {
|
|
175
389
|
if (node.inline)
|
|
176
390
|
return inlineToObsidian(node.inline);
|
|
177
391
|
if (node.html)
|
|
178
|
-
return
|
|
392
|
+
return htmlToObsidian(node.html);
|
|
179
393
|
return "";
|
|
180
394
|
}
|
|
181
395
|
// ── Callouts (lossless mapping) ─────────────────────────
|
|
182
396
|
function calloutToObsidian(node) {
|
|
183
|
-
|
|
397
|
+
// Shape-tolerant: the dogsbay-md parser emits `props.type` for `:::tip`
|
|
398
|
+
// directives; importers emit `props.variant`. Reading only `variant`
|
|
399
|
+
// demoted EVERY parsed callout to note — 2,194 of the OpenShift vault's
|
|
400
|
+
// 7,926 admonitions (all its tips/warnings/importants) mislabeled.
|
|
401
|
+
const variant = (node.props?.variant ?? node.props?.type) ?? "note";
|
|
184
402
|
const title = node.props?.title;
|
|
185
403
|
let opener = `> [!${variant}]`;
|
|
186
404
|
const autoTitle = variant.charAt(0).toUpperCase() + variant.slice(1);
|
|
405
|
+
// The title is inline Markdown on the callout's first line.
|
|
187
406
|
if (title && title !== autoTitle)
|
|
188
|
-
opener += ` ${title}`;
|
|
407
|
+
opener += ` ${esc(oneLine(title))}`;
|
|
189
408
|
if (title === null)
|
|
190
409
|
opener += "";
|
|
191
410
|
const inner = childrenToObsidian(node);
|
|
@@ -194,14 +413,16 @@ function calloutToObsidian(node) {
|
|
|
194
413
|
return `${opener}\n${prefixLines(inner, "> ")}`;
|
|
195
414
|
}
|
|
196
415
|
function detailsToObsidian(node) {
|
|
197
|
-
|
|
198
|
-
const
|
|
416
|
+
// Shape-tolerant variant read — see calloutToObsidian.
|
|
417
|
+
const variant = (node.props?.variant ?? node.props?.type) ?? "note";
|
|
418
|
+
// `summary` is dogsbay-md's alias for the title (`:::details{summary="…"}`)
|
|
419
|
+
const title = (node.props?.title ?? node.props?.summary);
|
|
199
420
|
const open = node.props?.open;
|
|
200
421
|
const foldMarker = open ? "+" : "-";
|
|
201
422
|
let opener = `> [!${variant}]${foldMarker}`;
|
|
202
423
|
const autoTitle = variant.charAt(0).toUpperCase() + variant.slice(1);
|
|
203
424
|
if (title && title !== autoTitle)
|
|
204
|
-
opener += ` ${title}`;
|
|
425
|
+
opener += ` ${esc(oneLine(title))}`;
|
|
205
426
|
const inner = childrenToObsidian(node);
|
|
206
427
|
if (!inner)
|
|
207
428
|
return opener;
|
|
@@ -236,8 +457,16 @@ function tabsToObsidian(node) {
|
|
|
236
457
|
codeBlockNestingDepth++;
|
|
237
458
|
const inner = node.children
|
|
238
459
|
.map((tab) => {
|
|
239
|
-
|
|
460
|
+
// The parser labels a tab with `label`; older/other shapes use `title` or
|
|
461
|
+
// `value`. Mirror format-astro's tolerant cascade — reading only `title`
|
|
462
|
+
// (the old obsidian behaviour) makes every tab render as the literal "Tab".
|
|
463
|
+
let title = tab.props?.title ||
|
|
464
|
+
tab.props?.label ||
|
|
465
|
+
tab.props?.value ||
|
|
466
|
+
"Tab";
|
|
240
467
|
const content = tabContentToObsidian(tab);
|
|
468
|
+
// One line: the plugin reads the header line by line.
|
|
469
|
+
title = oneLine(title);
|
|
241
470
|
if (!content)
|
|
242
471
|
return `tab: ${title}\n---`;
|
|
243
472
|
return `tab: ${title}\n---\n${content}`;
|
|
@@ -252,8 +481,12 @@ function tabContentToObsidian(node) {
|
|
|
252
481
|
}
|
|
253
482
|
// ── Code blocks ─────────────────────────────────────────
|
|
254
483
|
function codeToObsidian(node) {
|
|
255
|
-
|
|
256
|
-
|
|
484
|
+
// The parser emits `props.language` with the code text in `node.html`;
|
|
485
|
+
// importers use `props.lang`/`props.code`. Read BOTH (as format-astro does)
|
|
486
|
+
// — reading only `lang`/`code` produced an EMPTY, language-less fence for
|
|
487
|
+
// every code block parsed from real dogsbay-md.
|
|
488
|
+
const lang = node.props?.lang || node.props?.language || "";
|
|
489
|
+
const code = (node.props?.code ?? node.html ?? "").replace(/\n$/, "");
|
|
257
490
|
const title = node.props?.title;
|
|
258
491
|
const highlights = node.props?.highlights;
|
|
259
492
|
const annotations = node.props?.annotations;
|
|
@@ -292,7 +525,7 @@ function diagramToObsidian(node) {
|
|
|
292
525
|
// ── Math ────────────────────────────────────────────────
|
|
293
526
|
function mathBlockToObsidian(node) {
|
|
294
527
|
const latex = node.props?.latex ?? "";
|
|
295
|
-
return `$$\n${latex}\n$$`;
|
|
528
|
+
return `$$\n${markdownLatex(latex)}\n$$`;
|
|
296
529
|
}
|
|
297
530
|
// ── Tables (GFM pipe tables) ────────────────────────────
|
|
298
531
|
function tableToObsidian(node) {
|
|
@@ -366,11 +599,17 @@ function orderedListToObsidian(node) {
|
|
|
366
599
|
.map((child, i) => {
|
|
367
600
|
const num = start + i;
|
|
368
601
|
const text = nodeToObsidian(child);
|
|
369
|
-
|
|
602
|
+
// Continuation indent must reach the item's CONTENT column, which is
|
|
603
|
+
// the marker's own width: `10. ` is 4 wide, `100. ` is 5. A fixed
|
|
604
|
+
// 3-space indent detached every table/callout attached to item 10+
|
|
605
|
+
// (CommonMark: one column short of the content column ends the item).
|
|
606
|
+
const marker = `${num}. `;
|
|
607
|
+
const pad = " ".repeat(marker.length);
|
|
608
|
+
const first = `${marker}${text.split("\n")[0]}`;
|
|
370
609
|
const rest = text.split("\n").slice(1);
|
|
371
610
|
if (rest.length === 0)
|
|
372
611
|
return first;
|
|
373
|
-
return `${first}\n${rest.map((l) => (l ?
|
|
612
|
+
return `${first}\n${rest.map((l) => (l ? `${pad}${l}` : l)).join("\n")}`;
|
|
374
613
|
})
|
|
375
614
|
.join("\n");
|
|
376
615
|
}
|
|
@@ -389,18 +628,81 @@ function unorderedListToObsidian(node) {
|
|
|
389
628
|
.join("\n");
|
|
390
629
|
}
|
|
391
630
|
function listItemToObsidian(node) {
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
const parts =
|
|
395
|
-
//
|
|
396
|
-
|
|
397
|
-
|
|
631
|
+
// A list item's text is in `inline` (flat parser shape); nested blocks are
|
|
632
|
+
// in `children`. Reading only children returned "" for every plain bullet.
|
|
633
|
+
const parts = [];
|
|
634
|
+
// a task item's `[ ]` / `[x]` leads (Obsidian draws it as a checkbox)
|
|
635
|
+
const box = taskMarker(node);
|
|
636
|
+
if (node.inline && node.inline.length > 0)
|
|
637
|
+
parts.push(box + inlineToObsidian(node.inline));
|
|
638
|
+
else if (box)
|
|
639
|
+
parts.push(box.trimEnd());
|
|
640
|
+
if (node.children)
|
|
641
|
+
parts.push(...node.children.map(nodeToObsidian).filter(Boolean));
|
|
642
|
+
// Blank line between blocks so Obsidian treats consecutive blockquote-style
|
|
643
|
+
// elements (callouts, details) as separate.
|
|
644
|
+
return parts.filter(Boolean).join("\n\n");
|
|
398
645
|
}
|
|
399
646
|
// ── Definition lists (degraded) ─────────────────────────
|
|
647
|
+
/**
|
|
648
|
+
* Definition list → a `dogsbay-deflist` fence the vault plugin renders as a
|
|
649
|
+
* real `<dl>`.
|
|
650
|
+
*
|
|
651
|
+
* Obsidian has NO definition-list syntax. The previous emission used the
|
|
652
|
+
* common `**term**` / `: definition` convention, which Obsidian renders as
|
|
653
|
+
* literal text with a stray colon — 821 of 2,106 notes (28,946 lines) in a
|
|
654
|
+
* real OpenShift vault. And because the `dd` case flattened its body with
|
|
655
|
+
* `leafContent`, a definition carrying a TABLE was emitted as `: ` followed by
|
|
656
|
+
* pipe rows at column 0, so the table could not parse either.
|
|
657
|
+
*
|
|
658
|
+
* Definition bodies are serialized as full BLOCK markdown, so a table, list or
|
|
659
|
+
* code block inside a `dd` survives; the plugin renders each body through
|
|
660
|
+
* Obsidian's own markdown renderer.
|
|
661
|
+
*/
|
|
400
662
|
function deflistToObsidian(node) {
|
|
401
|
-
if (!node.children)
|
|
663
|
+
if (!node.children?.length)
|
|
402
664
|
return "";
|
|
403
|
-
|
|
665
|
+
const entries = [];
|
|
666
|
+
let current = null;
|
|
667
|
+
// A definition body may itself contain a fenced code block, so the wrapper
|
|
668
|
+
// alternates fence characters by nesting depth (same rule as steps/tabs).
|
|
669
|
+
const fence = codeBlockNestingDepth % 2 === 0 ? "~~~" : "```";
|
|
670
|
+
codeBlockNestingDepth++;
|
|
671
|
+
for (const child of node.children) {
|
|
672
|
+
if (child.type === "dt") {
|
|
673
|
+
if (current)
|
|
674
|
+
entries.push(current);
|
|
675
|
+
current = { term: leafContent(child).trim(), definitions: [] };
|
|
676
|
+
continue;
|
|
677
|
+
}
|
|
678
|
+
if (child.type === "dd") {
|
|
679
|
+
// Block children first — `leafContent` is the fallback for the flat
|
|
680
|
+
// `inline` shape the dogsbay-md parser produces.
|
|
681
|
+
const body = (childrenToObsidian(child) || leafContent(child)).trim();
|
|
682
|
+
if (!current)
|
|
683
|
+
current = { term: "", definitions: [] };
|
|
684
|
+
if (body)
|
|
685
|
+
current.definitions.push(body);
|
|
686
|
+
continue;
|
|
687
|
+
}
|
|
688
|
+
// Anything else inside a deflist keeps its content rather than vanishing.
|
|
689
|
+
const other = nodeToObsidian(child).trim();
|
|
690
|
+
if (!other)
|
|
691
|
+
continue;
|
|
692
|
+
if (!current)
|
|
693
|
+
current = { term: "", definitions: [] };
|
|
694
|
+
current.definitions.push(other);
|
|
695
|
+
}
|
|
696
|
+
if (current)
|
|
697
|
+
entries.push(current);
|
|
698
|
+
codeBlockNestingDepth--;
|
|
699
|
+
const body = entries
|
|
700
|
+
.map((e) => {
|
|
701
|
+
const defs = e.definitions.join("\n\n---\n").trim();
|
|
702
|
+
return defs ? `term: ${e.term}\n---\n${defs}` : `term: ${e.term}\n---`;
|
|
703
|
+
})
|
|
704
|
+
.join("\n\n");
|
|
705
|
+
return `${fence}dogsbay-deflist\n${body}\n${fence}`;
|
|
404
706
|
}
|
|
405
707
|
// ── Steps (numbered list) ───────────────────────────────
|
|
406
708
|
function stepsToObsidian(node) {
|
|
@@ -411,10 +713,18 @@ function stepsToObsidian(node) {
|
|
|
411
713
|
// Use dogsbay-steps element for richer rendering
|
|
412
714
|
const inner = node.children
|
|
413
715
|
.map((child) => {
|
|
414
|
-
|
|
716
|
+
// A `step` node carries its TITLE as its own inline content (the parser's
|
|
717
|
+
// ordered-list recovery), with the body in `children` — the same shape
|
|
718
|
+
// format-astro renders via leafContent. Reading `props.title` (the old
|
|
719
|
+
// behaviour) dropped every step title. `props.title` remains a fallback
|
|
720
|
+
// for shapes that carry it there.
|
|
721
|
+
const title = (child.inline && child.inline.length ? inlineToObsidian(child.inline) : "") ||
|
|
722
|
+
child.props?.title ||
|
|
723
|
+
"";
|
|
415
724
|
const content = childrenToObsidian(child);
|
|
416
725
|
if (title) {
|
|
417
|
-
|
|
726
|
+
const line = stepTitleLine(title);
|
|
727
|
+
return content ? `step: ${line}\n---\n${content}` : `step: ${line}\n---`;
|
|
418
728
|
}
|
|
419
729
|
// No title — just content (Starlight steps are titleless list items)
|
|
420
730
|
return content ? `step:\n---\n${content}` : `step:\n---`;
|
|
@@ -428,7 +738,8 @@ function headingToObsidian(node) {
|
|
|
428
738
|
const level = node.props?.level ?? 1;
|
|
429
739
|
const slug = node.props?.slug ?? "";
|
|
430
740
|
const hashes = "#".repeat(level);
|
|
431
|
-
const
|
|
741
|
+
const id = uniqueBlockId(obsidianBlockId(slug));
|
|
742
|
+
const blockId = id ? ` ^${id}` : "";
|
|
432
743
|
if (node.inline)
|
|
433
744
|
return `${hashes} ${inlineToObsidian(node.inline)}${blockId}`;
|
|
434
745
|
const text = childrenToObsidian(node) || (node.props?.text ?? "");
|
|
@@ -450,29 +761,119 @@ function referencesToObsidian(node) {
|
|
|
450
761
|
.join("\n");
|
|
451
762
|
}
|
|
452
763
|
function youtubeToObsidian(node) {
|
|
453
|
-
|
|
454
|
-
|
|
764
|
+
// The parser emits `props.id`; importers may use `props.videoId`. Read BOTH
|
|
765
|
+
// (as format-astro reads `props.id`) — reading only `videoId` produced a
|
|
766
|
+
// broken embed URL (`.../vi//maxres...`, `watch?v=`) for every parsed video.
|
|
767
|
+
const id = String(node.props?.id || node.props?.videoId || "").replace(/[^\w-]/g, "");
|
|
768
|
+
const title = (node.props?.title ?? "Video").replace(/[[\]\\\r\n]/g, " ");
|
|
455
769
|
return `[](https://www.youtube.com/watch?v=${id})`;
|
|
456
770
|
}
|
|
771
|
+
/**
|
|
772
|
+
* The icon value if a vault can show it, otherwise undefined.
|
|
773
|
+
*
|
|
774
|
+
* Renderable: an image path (the plugin builds an <img>), or an emoji — any
|
|
775
|
+
* non-ASCII glyph, which the plugin prints as text and which reads correctly.
|
|
776
|
+
* Not renderable: an ASCII slug naming an icon in a registry the vault does
|
|
777
|
+
* not have.
|
|
778
|
+
*/
|
|
779
|
+
function renderableVaultIcon(icon) {
|
|
780
|
+
if (/\.(svg|png|jpe?g|webp|avif)$/i.test(icon))
|
|
781
|
+
return vaultAssetPath(icon);
|
|
782
|
+
// eslint-disable-next-line no-control-regex
|
|
783
|
+
return /^[\x00-\x7F]+$/.test(icon) ? undefined : icon;
|
|
784
|
+
}
|
|
785
|
+
/** Give an internal card target the `.md` its exact-path lookup needs. */
|
|
786
|
+
function cardHref(href) {
|
|
787
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(href) || href.startsWith("#"))
|
|
788
|
+
return href;
|
|
789
|
+
return /\.[a-z0-9]+$/i.test(href) ? href : `${href}.md`;
|
|
790
|
+
}
|
|
457
791
|
function cardToObsidian(node) {
|
|
458
|
-
const title = node.props?.title || "";
|
|
459
|
-
const
|
|
792
|
+
const title = oneLine(node.props?.title || "");
|
|
793
|
+
const rawHref = node.props?.href;
|
|
794
|
+
const href = rawHref && isSafeHref(rawHref) ? oneLine(rawHref) : undefined;
|
|
460
795
|
const icon = node.props?.icon;
|
|
461
|
-
|
|
796
|
+
// The card is a fenced block, so its fence must differ from any fence its
|
|
797
|
+
// BODY writes — the same alternation tabs, steps and deflists use. A fixed
|
|
798
|
+
// triple-backtick fence (the old behaviour) was closed early by a code block
|
|
799
|
+
// in the body, and a card inside a backtick-fenced tabs/steps block closed
|
|
800
|
+
// that block instead; Starlight CardGrid pages hit both.
|
|
801
|
+
const fence = codeBlockNestingDepth % 2 === 0 ? "~~~" : "```";
|
|
802
|
+
codeBlockNestingDepth++;
|
|
803
|
+
let content = childrenToObsidian(node);
|
|
804
|
+
codeBlockNestingDepth--;
|
|
805
|
+
// The parser folds a one-paragraph body into `props.description` (a
|
|
806
|
+
// link-card, or Starlight's `<LinkCard description>` which maps to `card`),
|
|
807
|
+
// so the node has no children — read it, or the description is lost.
|
|
808
|
+
if (!content) {
|
|
809
|
+
const description = node.props?.description?.trim();
|
|
810
|
+
if (description)
|
|
811
|
+
content = esc(description);
|
|
812
|
+
}
|
|
462
813
|
const header = [];
|
|
463
814
|
if (title)
|
|
464
815
|
header.push(`title: ${title}`);
|
|
816
|
+
// `.md` REQUIRED here, unlike an ordinary markdown link.
|
|
817
|
+
//
|
|
818
|
+
// A `[text](path)` link is resolved by Obsidian's own linkpath lookup, which
|
|
819
|
+
// is forgiving about the extension. This href is consumed by the dogsbay-docs
|
|
820
|
+
// plugin, which resolves it with `vault.getFileByPath()` — an EXACT match.
|
|
821
|
+
// Extensionless, that returns null and the click does nothing at all: the
|
|
822
|
+
// cards rendered correctly and were simply inert, with no error anywhere.
|
|
465
823
|
if (href)
|
|
466
|
-
header.push(`href: ${obsidianHref(href)}`);
|
|
467
|
-
|
|
468
|
-
|
|
824
|
+
header.push(`href: ${cardHref(obsidianHref(href))}`);
|
|
825
|
+
// Only an icon the VAULT can render.
|
|
826
|
+
//
|
|
827
|
+
// The plugin draws an image when the icon is a file path and prints the
|
|
828
|
+
// value as text otherwise — which is right for an emoji and wrong for a
|
|
829
|
+
// registry NAME. Starlight names like `open-book`, `discord` and `x.com`
|
|
830
|
+
// have no file behind them and no registry in a vault, so they rendered as
|
|
831
|
+
// the literal word "discord" stacked one letter per line down the card's
|
|
832
|
+
// icon column.
|
|
833
|
+
//
|
|
834
|
+
// A name we cannot resolve is dropped rather than emitted: the card simply
|
|
835
|
+
// has no icon, which is what it would look like anyway if the reference
|
|
836
|
+
// worked and pointed at nothing.
|
|
837
|
+
const vaultIcon = icon ? renderableVaultIcon(icon) : undefined;
|
|
838
|
+
if (vaultIcon)
|
|
839
|
+
header.push(`icon: ${vaultIcon}`);
|
|
469
840
|
if (header.length === 0 && !content)
|
|
470
841
|
return "";
|
|
471
842
|
const meta = header.join("\n");
|
|
472
843
|
if (content) {
|
|
473
|
-
return
|
|
844
|
+
return `${fence}dogsbay-card\n${meta}\n---\n${content}\n${fence}`;
|
|
474
845
|
}
|
|
475
|
-
return
|
|
846
|
+
return `${fence}dogsbay-card\n${meta}\n${fence}`;
|
|
847
|
+
}
|
|
848
|
+
/**
|
|
849
|
+
* A `link-button` as the `dogsbay-link-button` block the dogsbay-docs plugin
|
|
850
|
+
* renders (`href:` / `variant:` / `---` / label). It used to be written as an
|
|
851
|
+
* ordinary Markdown link, which lost the call-to-action styling the plugin
|
|
852
|
+
* already had a processor for. With no safe href the label stays as text.
|
|
853
|
+
*/
|
|
854
|
+
function linkButtonToObsidian(node) {
|
|
855
|
+
const raw = node.props?.href ?? "";
|
|
856
|
+
const href = raw && isSafeHref(raw) ? obsidianHref(raw) : "";
|
|
857
|
+
const text = childrenToObsidian(node) || esc(href);
|
|
858
|
+
if (!href)
|
|
859
|
+
return text;
|
|
860
|
+
const fence = codeBlockNestingDepth % 2 === 0 ? "~~~" : "```";
|
|
861
|
+
const variant = node.props?.variant;
|
|
862
|
+
// Resolved by the plugin the way a card's is (exact vault path), so an
|
|
863
|
+
// internal target carries `.md`; project.ts then points it at the file
|
|
864
|
+
// that exists.
|
|
865
|
+
const header = [`href: ${cardHref(oneLine(href))}`];
|
|
866
|
+
if (variant)
|
|
867
|
+
header.push(`variant: ${oneLine(variant)}`);
|
|
868
|
+
return `${fence}dogsbay-link-button\n${header.join("\n")}\n---\n${oneLine(text)}\n${fence}`;
|
|
869
|
+
}
|
|
870
|
+
/**
|
|
871
|
+
* Raw HTML that is nothing but closing tags of PascalCase names — a JSX
|
|
872
|
+
* component's closer the MDX importer could not pair with its opener.
|
|
873
|
+
*/
|
|
874
|
+
function isOrphanComponentCloser(html) {
|
|
875
|
+
const t = html.trim();
|
|
876
|
+
return t.length > 0 && /^(<\/[A-Z][A-Za-z0-9]*\s*>\s*)+$/.test(t);
|
|
476
877
|
}
|
|
477
878
|
function cardsToObsidian(node) {
|
|
478
879
|
// Flatten cards/grid to a simple unordered list
|
|
@@ -487,13 +888,20 @@ function apiSymbolToObsidian(node) {
|
|
|
487
888
|
const level = node.type === "api-class" ? 3 : 4;
|
|
488
889
|
const hashes = "#".repeat(level);
|
|
489
890
|
const sig = node.props?.signature ?? "";
|
|
490
|
-
|
|
891
|
+
// The parser emits `bases` as a string; the RST/autodoc importer (and
|
|
892
|
+
// format-astro's read) may use a string[]. Tolerate both.
|
|
893
|
+
const basesRaw = node.props?.bases;
|
|
894
|
+
const bases = Array.isArray(basesRaw)
|
|
895
|
+
? basesRaw.join(", ")
|
|
896
|
+
: basesRaw;
|
|
491
897
|
const parts = [];
|
|
492
|
-
parts.push(`${hashes} ${kind}
|
|
898
|
+
parts.push(`${hashes} ${esc(kind)} ${codeSpan(name)}`);
|
|
493
899
|
if (bases)
|
|
494
|
-
parts.push(`Bases:
|
|
495
|
-
if (sig)
|
|
496
|
-
|
|
900
|
+
parts.push(`Bases: ${codeSpan(bases)}`);
|
|
901
|
+
if (sig) {
|
|
902
|
+
const fence = chooseFence(sig);
|
|
903
|
+
parts.push(`${fence}python\n${sig}\n${fence}`);
|
|
904
|
+
}
|
|
497
905
|
if (node.children)
|
|
498
906
|
parts.push(childrenToObsidian(node));
|
|
499
907
|
return parts.filter(Boolean).join("\n\n");
|
|
@@ -542,7 +950,7 @@ function apiDocToObsidian(node) {
|
|
|
542
950
|
// api-doc has HTML content in props.html — convert to markdown
|
|
543
951
|
const html = node.props?.html ?? "";
|
|
544
952
|
if (html)
|
|
545
|
-
return
|
|
953
|
+
return htmlToObsidian(html);
|
|
546
954
|
return childrenToObsidian(node);
|
|
547
955
|
}
|
|
548
956
|
function apiSourceToObsidian(node) {
|
|
@@ -557,6 +965,166 @@ function apiSourceToObsidian(node) {
|
|
|
557
965
|
if (code) {
|
|
558
966
|
parts.push(`\`\`\`python\n${code.replace(/\n$/, "")}\n\`\`\``);
|
|
559
967
|
}
|
|
968
|
+
else if (node.children && node.children.length > 0) {
|
|
969
|
+
// The dogsbay-md parser puts the fenced source into a child `code` node
|
|
970
|
+
// (props.language + node.html), not props.code — reading only props.code
|
|
971
|
+
// dropped the source block. Serialize children as a fallback; the
|
|
972
|
+
// props.code path stays for importers that carry it there.
|
|
973
|
+
const inner = childrenToObsidian(node);
|
|
974
|
+
if (inner)
|
|
975
|
+
parts.push(inner);
|
|
976
|
+
}
|
|
977
|
+
return parts.join("\n\n");
|
|
978
|
+
}
|
|
979
|
+
const objects = (v) => (Array.isArray(v) ? v.filter((x) => !!x && typeof x === "object") : []);
|
|
980
|
+
const text = (v) => (v == null ? "" : String(v));
|
|
981
|
+
/** A GFM table cell: one line, pipes escaped (GFM unescapes them inside code spans too). */
|
|
982
|
+
function cell(md) {
|
|
983
|
+
return oneLine(md).replace(/\|/g, "\\|");
|
|
984
|
+
}
|
|
985
|
+
/** A spec description (CommonMark, from someone else's file) in a table cell, inert. */
|
|
986
|
+
function descriptionCell(p) {
|
|
987
|
+
const parts = [neutralizeMarkdown(oneLine(text(p.description)))];
|
|
988
|
+
if (Array.isArray(p.enum) && p.enum.length)
|
|
989
|
+
parts.push(`One of: ${p.enum.map((v) => codeSpan(text(v))).join(", ")}`);
|
|
990
|
+
if (p.default !== undefined)
|
|
991
|
+
parts.push(`Default: ${codeSpan(typeof p.default === "string" ? p.default : JSON.stringify(p.default))}`);
|
|
992
|
+
if (p.example !== undefined)
|
|
993
|
+
parts.push(`Example: ${codeSpan(JSON.stringify(p.example))}`);
|
|
994
|
+
if (p.deprecated === true)
|
|
995
|
+
parts.push("*Deprecated.*");
|
|
996
|
+
return cell(parts.filter(Boolean).join(" "));
|
|
997
|
+
}
|
|
998
|
+
function typeCell(p) {
|
|
999
|
+
const t = text(p.type) || "any";
|
|
1000
|
+
return cell(codeSpan(p.format ? `${t} <${text(p.format)}>` : t) + (p.nullable === true ? " (nullable)" : ""));
|
|
1001
|
+
}
|
|
1002
|
+
function table(head, rows) {
|
|
1003
|
+
return [`| ${head.join(" | ")} |`, `|${head.map(() => " --- ").join("|")}|`, ...rows.map((r) => `| ${r.join(" | ")} |`)].join("\n");
|
|
1004
|
+
}
|
|
1005
|
+
/** Schema properties, nested ones flattened to `owner.id` / `tags[].name`, as the rows of one table. */
|
|
1006
|
+
function propertyRows(props, prefix = "", depth = 0) {
|
|
1007
|
+
if (depth > 10)
|
|
1008
|
+
return [];
|
|
1009
|
+
return props.flatMap((p) => {
|
|
1010
|
+
const name = `${prefix}${text(p.name)}`;
|
|
1011
|
+
const row = [cell(codeSpan(name)), typeCell(p), p.required === true ? "yes" : "", descriptionCell(p)];
|
|
1012
|
+
const nested = objects(p.properties).length
|
|
1013
|
+
? propertyRows(objects(p.properties), `${name}.`, depth + 1)
|
|
1014
|
+
: propertyRows(objects(p.items), `${name}[].`, depth + 1);
|
|
1015
|
+
return [row, ...nested];
|
|
1016
|
+
});
|
|
1017
|
+
}
|
|
1018
|
+
/** A foldable callout holding a code block (samples and examples). */
|
|
1019
|
+
function codeCallout(title, code, lang) {
|
|
1020
|
+
const fence = chooseFence(code);
|
|
1021
|
+
// The language is the spec's: a backtick or newline in it would keep the fence from opening, and the code would render as Markdown.
|
|
1022
|
+
const info = /^[\w#+.-]{1,40}$/.test(lang) ? lang : "";
|
|
1023
|
+
return `> [!example]- ${esc(oneLine(title))}\n${prefixLines(`${fence}${info}\n${code.replace(/\n$/, "")}\n${fence}`, "> ")}`;
|
|
1024
|
+
}
|
|
1025
|
+
/**
|
|
1026
|
+
* A spec description, inert; its own headings as bold lines (they would sit
|
|
1027
|
+
* in the note's outline under the operation, as on the web, where they are
|
|
1028
|
+
* bold paragraphs); and any fence it opens closed: an unclosed one would turn
|
|
1029
|
+
* the rest of the note into code.
|
|
1030
|
+
*/
|
|
1031
|
+
function specMarkdown(md) {
|
|
1032
|
+
let open;
|
|
1033
|
+
const lines = neutralizeMarkdown(md).split("\n").map((line) => {
|
|
1034
|
+
const m = /^ {0,3}(`{3,}|~{3,})/.exec(line);
|
|
1035
|
+
if (m) {
|
|
1036
|
+
if (!open)
|
|
1037
|
+
open = m[1];
|
|
1038
|
+
else if (m[1][0] === open[0] && m[1].length >= open.length && line.trim() === m[1])
|
|
1039
|
+
open = undefined;
|
|
1040
|
+
return line;
|
|
1041
|
+
}
|
|
1042
|
+
const h = open ? null : /^ {0,3}#{1,6}(?:[ \t]+(.*?))?(?:[ \t]+#+)?[ \t]*$/.exec(line);
|
|
1043
|
+
return h ? (h[1] ? `**${h[1]}**` : "") : line;
|
|
1044
|
+
});
|
|
1045
|
+
// Setext headings (a text line underlined with === or ---): the text bold, the underline gone.
|
|
1046
|
+
for (let k = 1, fence = false; k < lines.length; k++) {
|
|
1047
|
+
if (/^ {0,3}(`{3,}|~{3,})/.test(lines[k - 1]))
|
|
1048
|
+
fence = !fence;
|
|
1049
|
+
const prev = lines[k - 1];
|
|
1050
|
+
if (fence || !/^ {0,3}(=+|-+)[ \t]*$/.test(lines[k]))
|
|
1051
|
+
continue;
|
|
1052
|
+
if (!prev.trim() || /^ {0,3}([-*+>]|\d+[.)]|#|\*\*.*\*\*$)/.test(prev))
|
|
1053
|
+
continue;
|
|
1054
|
+
lines[k - 1] = `**${prev.trim()}**`;
|
|
1055
|
+
lines[k] = "";
|
|
1056
|
+
}
|
|
1057
|
+
const out = lines.join("\n");
|
|
1058
|
+
return open ? `${out}\n${open}` : out;
|
|
1059
|
+
}
|
|
1060
|
+
/** ExampleBlock's language for a response content type. */
|
|
1061
|
+
function exampleLang(contentType) {
|
|
1062
|
+
const base = contentType.split(";")[0].trim();
|
|
1063
|
+
if (!base || base.includes("json"))
|
|
1064
|
+
return "json";
|
|
1065
|
+
if (base.includes("xml"))
|
|
1066
|
+
return "xml";
|
|
1067
|
+
if (base.includes("yaml") || base.includes("yml"))
|
|
1068
|
+
return "yaml";
|
|
1069
|
+
if (base === "text/html")
|
|
1070
|
+
return "html";
|
|
1071
|
+
return /^text\/plain$|form/.test(base) ? "" : "json";
|
|
1072
|
+
}
|
|
1073
|
+
const SAMPLE_LABELS = {
|
|
1074
|
+
curl: "cURL", python: "Python", typescript: "TypeScript", javascript: "JavaScript", go: "Go",
|
|
1075
|
+
ruby: "Ruby", java: "Java", csharp: "C#", php: "PHP", rust: "Rust",
|
|
1076
|
+
};
|
|
1077
|
+
/**
|
|
1078
|
+
* An OpenAPI operation (`endpoint`, from format-openapi or the MDX adapters)
|
|
1079
|
+
* as vault Markdown: the method and path as a heading, the description,
|
|
1080
|
+
* tables for parameters, the request body and responses, and the code
|
|
1081
|
+
* samples and examples as folded callouts. Every description is the spec
|
|
1082
|
+
* author's Markdown, made inert (neutralizeMarkdown) as format-astro renders
|
|
1083
|
+
* it with raw HTML off. With no case here the default emitted the node's
|
|
1084
|
+
* (absent) children: every operation vanished from a vault without a word.
|
|
1085
|
+
*/
|
|
1086
|
+
function endpointToObsidian(node) {
|
|
1087
|
+
const p = (node.props ?? {});
|
|
1088
|
+
const method = (text(p.method) || "get").toUpperCase();
|
|
1089
|
+
const path = text(p.path);
|
|
1090
|
+
const parts = [`### ${codeSpan(method)} ${codeSpan(path)}`];
|
|
1091
|
+
if (p.summary)
|
|
1092
|
+
parts.push(esc(oneLine(text(p.summary))));
|
|
1093
|
+
if (p.deprecated === true)
|
|
1094
|
+
parts.push("> [!warning] Deprecated\n> This endpoint is deprecated.");
|
|
1095
|
+
if (typeof p.description === "string" && p.description.trim() && p.description !== p.summary) {
|
|
1096
|
+
parts.push(specMarkdown(p.description.trim()));
|
|
1097
|
+
}
|
|
1098
|
+
if (p.baseUrl)
|
|
1099
|
+
parts.push(`URL: ${codeSpan(text(p.baseUrl) + path)}`);
|
|
1100
|
+
const params = objects(p.parameters);
|
|
1101
|
+
if (params.length) {
|
|
1102
|
+
parts.push("**Parameters**", table(["Name", "In", "Type", "Required", "Description"], params.map((x) => [cell(codeSpan(text(x.name))), cell(esc(text(x.in))), typeCell(x), x.required === true ? "yes" : "", descriptionCell(x)])));
|
|
1103
|
+
}
|
|
1104
|
+
const body = objects(p.requestBody);
|
|
1105
|
+
if (body.length)
|
|
1106
|
+
parts.push("**Request body**", table(["Property", "Type", "Required", "Description"], propertyRows(body)));
|
|
1107
|
+
const responses = objects(p.responses);
|
|
1108
|
+
if (responses.length) {
|
|
1109
|
+
parts.push("**Responses**", table(["Status", "Description"], responses.map((r) => [cell(codeSpan(text(r.status))), cell(neutralizeMarkdown(oneLine(text(r.description))))])));
|
|
1110
|
+
for (const r of responses) {
|
|
1111
|
+
const schema = objects(r.schema);
|
|
1112
|
+
if (!schema.length)
|
|
1113
|
+
continue;
|
|
1114
|
+
parts.push(`> [!info]- ${esc(`Response ${text(r.status)} schema`)}\n${prefixLines(table(["Property", "Type", "Required", "Description"], propertyRows(schema)), "> ")}`);
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
for (const s of objects(p.codeSamples)) {
|
|
1118
|
+
const lang = text(s.language);
|
|
1119
|
+
parts.push(codeCallout(text(s.label) || SAMPLE_LABELS[lang] || lang, text(s.code), lang === "curl" ? "bash" : lang));
|
|
1120
|
+
}
|
|
1121
|
+
if (p.requestBodyExample !== undefined)
|
|
1122
|
+
parts.push(codeCallout("Request body example", JSON.stringify(p.requestBodyExample, null, 2), "json"));
|
|
1123
|
+
for (const r of responses) {
|
|
1124
|
+
if (r.example === undefined)
|
|
1125
|
+
continue;
|
|
1126
|
+
parts.push(codeCallout(`Response ${text(r.status)} example`, JSON.stringify(r.example, null, 2), exampleLang(text(r.contentType))));
|
|
1127
|
+
}
|
|
560
1128
|
return parts.join("\n\n");
|
|
561
1129
|
}
|
|
562
1130
|
// ── Footnotes ───────────────────────────────────────────
|
|
@@ -566,8 +1134,13 @@ function footnoteListToObsidian(node) {
|
|
|
566
1134
|
return node.children.map(nodeToObsidian).join("\n");
|
|
567
1135
|
}
|
|
568
1136
|
function footnoteDefToObsidian(node) {
|
|
569
|
-
const label = node.props?.label ?? "1";
|
|
570
|
-
|
|
1137
|
+
const label = footnoteLabel(String(node.props?.label ?? "1"));
|
|
1138
|
+
// The dogsbay-md parser puts the definition body on `inline` (flat
|
|
1139
|
+
// shape); reading only `children` dropped every footnote body — the
|
|
1140
|
+
// three-TreeNode-shapes lesson documented at leafContent below.
|
|
1141
|
+
const content = node.inline && node.inline.length > 0
|
|
1142
|
+
? inlineToObsidian(node.inline)
|
|
1143
|
+
: childrenToObsidian(node);
|
|
571
1144
|
return `[^${label}]: ${content}`;
|
|
572
1145
|
}
|
|
573
1146
|
// ── Utilities ───────────────────────────────────────────
|
|
@@ -576,50 +1149,77 @@ function childrenToObsidian(node) {
|
|
|
576
1149
|
return "";
|
|
577
1150
|
return node.children.map(nodeToObsidian).filter(Boolean).join("\n\n");
|
|
578
1151
|
}
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
1152
|
+
/**
|
|
1153
|
+
* Text-bearing content across all THREE TreeNode shapes.
|
|
1154
|
+
*
|
|
1155
|
+
* Was a local re-derivation handling only two of them (flat `inline` and
|
|
1156
|
+
* `children`). It now goes through the core's `renderLeaf`, which adds the
|
|
1157
|
+
* third — a node carrying only `html`, or a single wrapped `prose` child
|
|
1158
|
+
* carrying `html` with no `inline` (what the MDX/Starlight/MkDocs importers
|
|
1159
|
+
* emit). Reading only the first two rendered those nodes EMPTY; that is the
|
|
1160
|
+
* bug that shipped in the Docusaurus exporter's first version and dropped
|
|
1161
|
+
* every paragraph body on a real Mintlify conversion.
|
|
1162
|
+
*
|
|
1163
|
+
* The `join` is "\n\n" to preserve this serializer's paragraph spacing (the
|
|
1164
|
+
* core defaults to "\n").
|
|
1165
|
+
*/
|
|
1166
|
+
function leafContent(node) {
|
|
1167
|
+
return renderLeaf(node, {
|
|
1168
|
+
inline: inlineToObsidian,
|
|
1169
|
+
children: (children) => children.map(nodeToObsidian).filter(Boolean).join("\n\n"),
|
|
1170
|
+
html: htmlToObsidian,
|
|
1171
|
+
join: "\n\n",
|
|
1172
|
+
});
|
|
591
1173
|
}
|
|
592
1174
|
/**
|
|
593
|
-
*
|
|
594
|
-
*
|
|
1175
|
+
* Render pre-rendered prose HTML as Obsidian markdown.
|
|
1176
|
+
*
|
|
1177
|
+
* `stripHtml` alone DELETES tags, so every `<code>`, `<strong>`, `<em>` and
|
|
1178
|
+
* `<a>` an importer hands us became plain text. That is invisible for sources
|
|
1179
|
+
* whose prose nodes also carry `inline` (mkdocs), and catastrophic for those
|
|
1180
|
+
* that carry ONLY html — the MDX/Mintlify shape. Measured on a full Mintlify
|
|
1181
|
+
* export before this fix: 38,994 inline code spans in the source became 6,864
|
|
1182
|
+
* in the vault, and 14,548 bold runs became 3,465. Roughly 80% of inline
|
|
1183
|
+
* formatting, silently gone, on the export we point at as the back-out.
|
|
1184
|
+
*
|
|
1185
|
+
* `inlineHtmlToMarkdown` converts the unambiguous inline tags to real markdown
|
|
1186
|
+
* first; `stripHtml` then removes whatever markup is left and decodes entities.
|
|
595
1187
|
*/
|
|
1188
|
+
function htmlToObsidian(html) {
|
|
1189
|
+
// Applied HERE because this is the shared path for prose, html-container and
|
|
1190
|
+
// inline HTML. Raw `<img src="/_assets/…">` most often arrives inline inside
|
|
1191
|
+
// a paragraph, not as a standalone `html` node — 39 vault images stayed
|
|
1192
|
+
// site-rooted when only the block case was covered.
|
|
1193
|
+
// BEFORE the html→markdown conversion: `inlineHtmlToMarkdown` turns
|
|
1194
|
+
// `<img src>` into ``, after which there is no `src=` attribute
|
|
1195
|
+
// left to rewrite.
|
|
1196
|
+
// stripHtml decodes entities: an `<img onerror=…>` that was TEXT
|
|
1197
|
+
// came back tag-shaped, and Obsidian renders inline HTML.
|
|
1198
|
+
// Code spans are left as written: a backslash there would show.
|
|
1199
|
+
return mapOutsideCodeSpans(stripHtml(inlineHtmlToMarkdown(vaultifyHtmlAssets(html), { flatten: true })), (t) => t.replace(/<(?=[A-Za-z/!?])/g, "\\<"));
|
|
1200
|
+
}
|
|
1201
|
+
/** Literal text as vault Markdown (`\<`, which reads better than `<`
|
|
1202
|
+
* in a file people edit). */
|
|
1203
|
+
function esc(text) {
|
|
1204
|
+
return escapeMarkdownText(text, { style: "backslash" });
|
|
1205
|
+
}
|
|
1206
|
+
/** Text for a one-line header or label: newlines folded to spaces. */
|
|
1207
|
+
function oneLine(s) {
|
|
1208
|
+
return s.replace(/\s*[\r\n]+\s*/g, " ");
|
|
1209
|
+
}
|
|
1210
|
+
/** Obsidian's code fence, via the core's shared longest-run picker. */
|
|
596
1211
|
function chooseFence(content) {
|
|
597
|
-
|
|
598
|
-
const matches = content.match(/`{3,}/g);
|
|
599
|
-
if (matches) {
|
|
600
|
-
for (const m of matches) {
|
|
601
|
-
if (m.length > max)
|
|
602
|
-
max = m.length;
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
return "`".repeat(max + 1);
|
|
1212
|
+
return pickCodeFence(content);
|
|
606
1213
|
}
|
|
607
1214
|
/**
|
|
608
|
-
*
|
|
609
|
-
*
|
|
1215
|
+
* A step title on its `step:` line, which the plugin renders as Markdown: one line, and a start
|
|
1216
|
+
* that would make it a block (`1. Install` a list, `# x` a heading, `> x` a quote, `---` a rule)
|
|
1217
|
+
* escaped, so it stays the title's text (review, 2026-11-02).
|
|
610
1218
|
*/
|
|
611
|
-
function
|
|
612
|
-
return
|
|
613
|
-
.replace(
|
|
614
|
-
.replace(
|
|
615
|
-
.replace(
|
|
616
|
-
.replace(
|
|
617
|
-
|
|
618
|
-
.replace(/&/g, "&")
|
|
619
|
-
.replace(/</g, "<")
|
|
620
|
-
.replace(/>/g, ">")
|
|
621
|
-
.replace(/"/g, '"')
|
|
622
|
-
.replace(/'/g, "'")
|
|
623
|
-
.trim();
|
|
624
|
-
}
|
|
625
|
-
//# sourceMappingURL=serialize.js.map
|
|
1219
|
+
function stepTitleLine(title) {
|
|
1220
|
+
return title
|
|
1221
|
+
.replace(/\s*\n\s*/g, " ")
|
|
1222
|
+
.replace(/^(\d{1,9})([.)])(?=\s|$)/, "$1\\$2")
|
|
1223
|
+
.replace(/^([>+]|[-*](?=\s)|#{1,6}(?=\s|$))/, "\\$1")
|
|
1224
|
+
.replace(/^([-*_])(?=(?:\s*\1){2,}\s*$)/, "\\$1");
|
|
1225
|
+
}
|