@remigius42/morg 0.8.0 → 0.9.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.
@@ -1,5 +1,8 @@
1
1
  import { consumesBracedScripts } from "../core/bracedScripts.js";
2
+ import { readsAsLineSyntax } from "../core/lineSyntax.js";
3
+ import { ZERO_WIDTH_SPACE } from "../core/markupBoundary.js";
2
4
  import { isDrawerStart, isOrgBlockStart, orgElementEnd } from "../core/passthroughSource.js";
5
+ import { readVanillaMarkdownOutline } from "./logseqVanillaMarkdown.js";
3
6
  // Logseq stores a page as an outline of blocks, each block a content
4
7
  // string it parses on its own: org writes a block as its level's stars,
5
8
  // a space and the content (an empty block as the bare stars), Markdown
@@ -106,68 +109,82 @@ function convertPage(lines, convert, preset) {
106
109
  ? [`${convert(lines.join("\n"), preset).replace(/\n+$/, "")}\n`]
107
110
  : [];
108
111
  }
109
- /**
110
- * Converts a Logseq org page to Logseq Markdown, block by block.
111
- * @param org The org page.
112
- * @param convert The core's fragment converter.
113
- * @param presets The presets for the page properties and for a block.
114
- * @returns The Markdown page.
115
- */
116
- export function orgOutlineToMarkdown(org, convert, presets) {
117
- const { page, blocks } = splitBlocks(org.replace(/\r?\n$/, "").split(/\r?\n/), line => {
118
- const match = ORG_BLOCK_RE.exec(line);
119
- return match ? [match[1]?.length ?? 0, match[2] ?? ""] : null;
120
- });
121
- return [
122
- ...convertPage(page, convert, presets.page),
123
- ...blocks.map(block => orgBlockToMarkdown(block, convert, presets.block))
124
- ]
125
- .join("\n")
126
- .concat("\n");
127
- }
128
112
  // org→md reads a block's bare `_` and `^` as text, as md→org writes
129
113
  // them without the setting that would say so
130
114
  function withBracedScripts(lines) {
131
115
  const braced = [BRACED_SCRIPTS_LINE, ...lines];
132
116
  return consumesBracedScripts(braced.join("\n")) ? braced : lines;
133
117
  }
134
- function orgBlockToMarkdown(block, convert, preset) {
135
- const [first = "", ...rest] = block.lines;
136
- // content that starts with the block's properties puts their drawer
137
- // on the headline line; its heading level is then a property too
118
+ // Logseq org: a block's properties drawer, as properties; a heading
119
+ // level is one of them, but for content that starts with the drawer
120
+ function readOrgBlock({ level, lines }) {
121
+ const [first = "", ...rest] = lines;
138
122
  const metaFirst = isDrawerStart(first);
139
- const { meta, body } = takeMeta(metaFirst ? block.lines : rest, () => false);
140
- let heading = "";
141
- const metaLines = meta.flatMap(lines => {
142
- if (lines[0] !== ":PROPERTIES:") {
143
- return logbook(lines, "*");
123
+ const { meta: groups, body } = takeMeta(metaFirst ? lines : rest, () => false);
124
+ let heading = 0;
125
+ const meta = groups.flatMap((group) => {
126
+ if (group[0] !== ":PROPERTIES:") {
127
+ return [{ lines: group }];
144
128
  }
145
- return lines.slice(1, -1).flatMap(line => {
129
+ return group.slice(1, -1).flatMap(line => {
146
130
  const [, key = "", value = ""] = ORG_PROPERTY_RE.exec(line) ?? [];
147
131
  if (!metaFirst && key === "heading" && /^[1-6]$/.test(value)) {
148
- heading = "#".repeat(Number(value));
132
+ heading = Number(value);
149
133
  return [];
150
134
  }
151
- return [`${key}::${value ? ` ${value}` : ""}`];
135
+ return [{ key, value }];
152
136
  });
153
137
  });
154
- const content = convertContent(withBracedScripts(metaFirst ? body : [first, ...body]), convert, preset);
155
- const [title = "", ...more] = arrange(metaFirst, metaLines, content);
156
- const head = [heading, title].filter(Boolean).join(" ");
157
- const indent = "\t".repeat(block.level - 1);
158
- return [
159
- `${indent}-${head ? ` ${head}` : ""}`,
160
- ...more.map(line => `${indent} ${line}`)
161
- ].join("\n");
138
+ return {
139
+ level,
140
+ heading,
141
+ metaFirst,
142
+ meta,
143
+ content: metaFirst ? body : [first, ...body]
144
+ };
162
145
  }
163
- /**
164
- * Converts a Logseq Markdown page to Logseq org, block by block.
165
- * @param markdown The Markdown page.
166
- * @param convert The core's fragment converter.
167
- * @param presets The presets for the page properties and for a block.
168
- * @returns The org page.
169
- */
170
- export function markdownOutlineToOrg(markdown, convert, presets) {
146
+ function readOrgOutline(org) {
147
+ const { page, blocks } = splitBlocks(org.replace(/\r?\n$/, "").split(/\r?\n/), line => {
148
+ const match = ORG_BLOCK_RE.exec(line);
149
+ return match ? [match[1]?.length ?? 0, match[2] ?? ""] : null;
150
+ });
151
+ return { page, blocks: blocks.map(readOrgBlock) };
152
+ }
153
+ // a continuation line sits two spaces inside its bullet
154
+ function dedent(line, level) {
155
+ const indent = `${"\t".repeat(level - 1)} `;
156
+ return line.startsWith(indent)
157
+ ? line.slice(indent.length)
158
+ : line.trim()
159
+ ? line
160
+ : "";
161
+ }
162
+ // `## title`: the heading level and the title
163
+ function mdHeading(line) {
164
+ const match = MD_HEADING_RE.exec(line);
165
+ return match ? [match[1]?.length ?? 0, match[2] ?? ""] : [0, line];
166
+ }
167
+ function readMarkdownBlock({ level, lines: source }) {
168
+ const [first = "", ...rest] = source;
169
+ const lines = [first, ...rest.map(line => dedent(line, level))];
170
+ const metaFirst = MD_PROPERTY_RE.test(first);
171
+ const [heading, titleLine] = metaFirst ? [0, ""] : mdHeading(first);
172
+ const { meta: groups, body } = takeMeta(metaFirst ? lines : lines.slice(1), line => MD_PROPERTY_RE.test(line));
173
+ const meta = groups.map((group) => {
174
+ const property = MD_PROPERTY_RE.exec(group[0] ?? "");
175
+ return property
176
+ ? { key: property[1] ?? "", value: property[2] ?? "" }
177
+ : { lines: group };
178
+ });
179
+ return {
180
+ level,
181
+ heading,
182
+ metaFirst,
183
+ meta,
184
+ content: metaFirst ? body : [titleLine, ...body]
185
+ };
186
+ }
187
+ function readMarkdownOutline(markdown) {
171
188
  const lines = markdown.replace(/\r?\n$/, "").split(/\r?\n/);
172
189
  // a leading frontmatter is page content, its `- ` lines yaml items
173
190
  const frontmatter = lines.slice(0, lines[0] === "---" ? lines.indexOf("---", 1) + 1 : 0);
@@ -183,38 +200,45 @@ export function markdownOutlineToOrg(markdown, convert, presets) {
183
200
  // writes a page's first block if it is a heading
184
201
  return !fenced && MD_HEADING_RE.test(line) ? [1, line] : null;
185
202
  });
186
- page.unshift(...frontmatter);
187
- return [
188
- ...convertPage(page, convert, presets.page),
189
- ...blocks.map(block => mdBlockToOrg(block, convert, presets.block))
190
- ]
191
- .join("\n")
192
- .concat("\n");
203
+ return {
204
+ page: [...frontmatter, ...page],
205
+ blocks: blocks.map(readMarkdownBlock)
206
+ };
193
207
  }
194
- // a continuation line sits two spaces inside its bullet
195
- function dedent(line, level) {
196
- const indent = `${"\t".repeat(level - 1)} `;
197
- return line.startsWith(indent)
198
- ? line.slice(indent.length)
199
- : line.trim()
200
- ? line
201
- : "";
208
+ // a block's content's first line, its meta lines, then the rest of the
209
+ // content; content that starts with the meta lines keeps them first
210
+ function arrange(metaFirst, meta, content) {
211
+ return metaFirst
212
+ ? [...meta, ...content]
213
+ : [content[0] ?? "", ...meta, ...content.slice(1)];
214
+ }
215
+ function propertyLine({ key, value }) {
216
+ return `${key}::${value ? ` ${value}` : ""}`;
217
+ }
218
+ function writeMarkdownBlock(block) {
219
+ const meta = block.meta.flatMap(item => "lines" in item ? logbook(item.lines, "*") : [propertyLine(item)]);
220
+ const [title = "", ...more] = arrange(block.metaFirst, meta, block.content);
221
+ const head = ["#".repeat(block.heading), title].filter(Boolean).join(" ");
222
+ const indent = "\t".repeat(block.level - 1);
223
+ return [
224
+ `${indent}-${head ? ` ${head}` : ""}`,
225
+ ...more.map(line => `${indent} ${line}`)
226
+ ].join("\n");
202
227
  }
203
- // the md meta lines in org: `key::` lines become the property drawer,
204
- // where the first of them was, or below the planning lines
228
+ // the meta in org: properties become the property drawer, where the
229
+ // first of them was, or below the planning lines
205
230
  function orgMetaLines(meta, heading) {
206
231
  const properties = heading ? [`:heading: ${heading}`] : [];
207
232
  const lines = [];
208
- for (const group of meta) {
209
- const property = MD_PROPERTY_RE.exec(group[0] ?? "");
210
- if (!property) {
211
- lines.push(...logbook(group, "-"));
233
+ for (const item of meta) {
234
+ if ("lines" in item) {
235
+ lines.push(...logbook(item.lines, "-"));
212
236
  continue;
213
237
  }
214
238
  if (!lines.includes(null)) {
215
239
  lines.push(null);
216
240
  }
217
- properties.push(`:${property[1]}:${property[2] ? ` ${property[2]}` : ""}`);
241
+ properties.push(`:${item.key}:${item.value ? ` ${item.value}` : ""}`);
218
242
  }
219
243
  if (properties.length && !lines.includes(null)) {
220
244
  lines.splice(lines.filter(line => PLANNING_RE.test(line ?? "")).length, 0, null);
@@ -222,30 +246,183 @@ function orgMetaLines(meta, heading) {
222
246
  const drawer = [":PROPERTIES:", ...properties, ":END:"];
223
247
  return lines.flatMap(line => (line === null ? drawer : [line]));
224
248
  }
225
- // a block's content's first line, its meta lines, then the rest of the
226
- // content; content that starts with the meta lines keeps them first
227
- function arrange(metaFirst, meta, content) {
228
- return metaFirst
229
- ? [...meta, ...content]
230
- : [content[0] ?? "", ...meta, ...content.slice(1)];
231
- }
232
- // `## title`: the heading level and the title
233
- function mdHeading(line) {
234
- const match = MD_HEADING_RE.exec(line);
235
- return match ? [match[1]?.length ?? 0, match[2] ?? ""] : [0, line];
236
- }
237
- function mdBlockToOrg(block, convert, preset) {
238
- const [first = "", ...rest] = block.lines;
239
- const lines = [first, ...rest.map(line => dedent(line, block.level))];
240
- // content that starts with properties: their drawer opens on the
241
- // headline line
242
- const metaFirst = MD_PROPERTY_RE.test(first);
243
- const [heading, titleLine] = metaFirst ? [0, ""] : mdHeading(first);
244
- const { meta, body } = takeMeta(metaFirst ? lines : lines.slice(1), line => MD_PROPERTY_RE.test(line));
245
- const content = convertMarkdownContent(metaFirst ? body : [titleLine, ...body], convert, preset).filter(line => line !== BRACED_SCRIPTS_LINE);
246
- const [title = "", ...more] = arrange(metaFirst, orgMetaLines(meta, heading), content);
249
+ function writeOrgBlock(block) {
250
+ const [title = "", ...more] = arrange(block.metaFirst, orgMetaLines(block.meta, block.heading), block.content);
247
251
  return [
248
252
  `${"*".repeat(block.level)}${title ? ` ${title}` : ""}`,
249
253
  ...more
250
254
  ].join("\n");
251
255
  }
256
+ function writeOutline({ page, blocks }, writeBlock) {
257
+ return [...page, ...blocks.map(writeBlock)].join("\n").concat("\n");
258
+ }
259
+ // a block's title is a headline's, which Logseq reads as inline text
260
+ // only; converted with the lines below it, it must not read as a list,
261
+ // a headline or a fixed-width line, so an escape the core drops keeps
262
+ // it text (ADR 0006)
263
+ function inlineTitle(block) {
264
+ const [title = "", ...rest] = block.content;
265
+ return !block.metaFirst && readsAsLineSyntax(title)
266
+ ? [`${ZERO_WIDTH_SPACE}${title}`, ...rest]
267
+ : block.content;
268
+ }
269
+ // a title written from Markdown text keeps it text with an escape that
270
+ // a headline needs none of
271
+ function dropTitleEscape(block) {
272
+ const [title = "", ...rest] = block.content;
273
+ return block.metaFirst || !title.startsWith(ZERO_WIDTH_SPACE)
274
+ ? block
275
+ : { ...block, content: [title.slice(ZERO_WIDTH_SPACE.length), ...rest] };
276
+ }
277
+ // a task marker Logseq shows as a checkbox (0.10, block-checkbox), and
278
+ // the text after it; TODO and DONE are the checkbox's own
279
+ const CHECKBOX_MARKER_RE = /^(TODO|DONE|NOW|LATER|DOING|IN-PROGRESS|WAIT|WAITING) (.*)$/;
280
+ // Vanilla Markdown: a task as a task item, marked as Logseq shows it,
281
+ // so the marker comes back; CANCELED, shown without one, stays text
282
+ function vanillaTitle(title) {
283
+ const [, marker, text] = CHECKBOX_MARKER_RE.exec(title) ?? [];
284
+ if (!marker) {
285
+ return title;
286
+ }
287
+ if (marker === "DONE") {
288
+ return `[x] ${text}`;
289
+ }
290
+ return marker === "TODO" ? `[ ] ${text}` : `[ ] ${marker} ${text}`;
291
+ }
292
+ const PLANNING_ENTRY_RE = /(SCHEDULED|DEADLINE): ([<[][^>\]]*[>\]])/g;
293
+ // Vanilla Markdown: planning and properties as key:: lines, under the
294
+ // org-ism names; a drawer and Logseq's view state have no form there
295
+ function vanillaMeta(meta, context) {
296
+ return meta.flatMap(item => {
297
+ if ("key" in item) {
298
+ if (item.key === "collapsed") {
299
+ context.onWarning?.("collapsed is Logseq's view state; dropped");
300
+ return [];
301
+ }
302
+ return [propertyLine(item)];
303
+ }
304
+ const [first = ""] = item.lines;
305
+ if (isDrawerStart(first)) {
306
+ context.onWarning?.(`a ${first.slice(1, -1)} drawer has no Vanilla Markdown form; dropped`);
307
+ return [];
308
+ }
309
+ return [...first.matchAll(PLANNING_ENTRY_RE)].map(([, key = "", value]) => {
310
+ const canonical = key.toLowerCase();
311
+ return `${context.orgismKeys?.[canonical] ?? canonical}:: ${value}`;
312
+ });
313
+ });
314
+ }
315
+ // a block Logseq shows numbered, as a run of its siblings
316
+ function isNumbered(item) {
317
+ return ("key" in item &&
318
+ item.key === "logseq.order-list-type" &&
319
+ item.value === "number");
320
+ }
321
+ function vanillaBlock(block, indent, bullet, context) {
322
+ const [first = "", ...more] = arrange(block.metaFirst, vanillaMeta(block.meta.filter(item => !isNumbered(item)), context), block.content);
323
+ const hashes = "#".repeat(block.heading);
324
+ // a heading no list holds is a heading of its own
325
+ if (block.heading && !indent) {
326
+ const body = more.length ? `\n\n${more.join("\n")}` : "";
327
+ return { heading: true, text: `${hashes} ${first}${body}` };
328
+ }
329
+ const title = [hashes, vanillaTitle(first)].filter(Boolean).join(" ");
330
+ return {
331
+ heading: false,
332
+ text: [
333
+ `${indent}${bullet}${title ? ` ${title}` : ""}`,
334
+ ...more.map(line => line ? `${indent}${" ".repeat(bullet.length + 1)}${line}` : "")
335
+ ].join("\n")
336
+ };
337
+ }
338
+ // Vanilla Markdown: a block is a list item, its children nested two
339
+ // spaces further in; under a heading block they start a new list
340
+ // (ADR 0006)
341
+ function writeVanillaMarkdownOutline({ page, blocks }, context) {
342
+ // a parent's level, its children's indent and their numbered run
343
+ const parents = [{ level: 0, indent: "", run: 0 }];
344
+ const written = blocks.map(block => {
345
+ while (parents.length > 1 && (parents.at(-1)?.level ?? 0) >= block.level) {
346
+ parents.pop();
347
+ }
348
+ const parent = parents.at(-1) ?? { level: 0, indent: "", run: 0 };
349
+ parent.run = block.meta.some(isNumbered) ? parent.run + 1 : 0;
350
+ const bullet = parent.run ? `${parent.run}.` : "-";
351
+ const result = vanillaBlock(block, parent.indent, bullet, context);
352
+ parents.push({
353
+ level: block.level,
354
+ indent: result.heading
355
+ ? ""
356
+ : `${parent.indent}${" ".repeat(bullet.length + 1)}`,
357
+ run: 0
358
+ });
359
+ return result;
360
+ });
361
+ const body = written
362
+ .map((block, i) => i && (block.heading || written[i - 1]?.heading)
363
+ ? `\n${block.text}`
364
+ : block.text)
365
+ .join("\n");
366
+ return [
367
+ ...page.map(text => text.replace(/\n+$/, "")),
368
+ ...(body ? [body] : [])
369
+ ]
370
+ .join("\n\n")
371
+ .concat("\n");
372
+ }
373
+ /**
374
+ * Converts a Logseq org page to Markdown, block by block: Logseq's,
375
+ * or Vanilla where the preset is on the input side only.
376
+ * @param org The org page.
377
+ * @param convert The core's fragment converter.
378
+ * @param presets The presets for the page properties and for a block.
379
+ * @param context The side the preset is on.
380
+ * @returns The Markdown page.
381
+ */
382
+ export function orgOutlineToMarkdown(org, convert, presets, context) {
383
+ const { page, blocks } = readOrgOutline(org);
384
+ const vanilla = context.side === "input";
385
+ const pageLines = vanilla ? (presets.vanillaPage?.(page) ?? page) : page;
386
+ const convertCarried = (fragment, preset) => convert(fragment, preset, vanilla ? presets.vanillaInline : undefined);
387
+ const outline = {
388
+ page: convertPage(pageLines, convertCarried, presets.page),
389
+ blocks: blocks.map(block => ({
390
+ ...block,
391
+ content: convertContent(withBracedScripts(vanilla ? inlineTitle(block) : block.content), convertCarried, presets.block)
392
+ }))
393
+ };
394
+ return vanilla
395
+ ? writeVanillaMarkdownOutline(outline, context)
396
+ : writeOutline(outline, writeMarkdownBlock);
397
+ }
398
+ /**
399
+ * Converts a Logseq Markdown page to Logseq org, block by block.
400
+ * @param markdown The Markdown page.
401
+ * @param convert The core's fragment converter.
402
+ * @param presets The presets for the page properties and for a block.
403
+ * @param context The side the preset is on.
404
+ * @returns The org page.
405
+ */
406
+ export function markdownOutlineToOrg(markdown, convert, presets, context) {
407
+ const vanilla = context.side === "output";
408
+ const { page, blocks } = vanilla
409
+ ? readVanillaMarkdownOutline(markdown, context)
410
+ : readMarkdownOutline(markdown);
411
+ // Vanilla Markdown carries Logseq's syntax as Logseq Markdown writes
412
+ // it; Vanilla org is written as Logseq org, which it is (ADR 0006)
413
+ const convertCarried = (fragment, preset) => convert(fragment, preset, context.side === "both" || !preset
414
+ ? undefined
415
+ : vanilla
416
+ ? (presets.vanillaReader?.(preset) ?? preset)
417
+ : preset);
418
+ return writeOutline({
419
+ page: convertPage(page, convertCarried, presets.page),
420
+ blocks: blocks.map(block => {
421
+ const converted = {
422
+ ...block,
423
+ content: convertMarkdownContent(block.content, convertCarried, presets.block).filter(line => line !== BRACED_SCRIPTS_LINE)
424
+ };
425
+ return vanilla ? dropTitleEscape(converted) : converted;
426
+ })
427
+ }, writeOrgBlock);
428
+ }
@@ -0,0 +1,9 @@
1
+ import type { Outline } from "./logseqOutline.js";
2
+ import type { ConversionContext } from "./types.js";
3
+ /**
4
+ * Reads a Vanilla Markdown page into Logseq's outline.
5
+ * @param markdown The Markdown page.
6
+ * @param context The conversion's org-ism key names.
7
+ * @returns The outline, its blocks' content in Markdown.
8
+ */
9
+ export declare function readVanillaMarkdownOutline(markdown: string, context: ConversionContext): Outline;