roam-research-mcp 6.2.0 → 6.3.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/README.md +8 -0
- package/build/cli/batch/resolver.js +2 -1
- package/build/cli/batch/translator.js +29 -57
- package/build/cli/commands/batch.js +14 -0
- package/build/cli/commands/save.js +6 -53
- package/build/markdown-utils.js +12 -4
- package/build/tools/operations/outline.js +44 -21
- package/build/tools/operations/pages.js +15 -7
- package/build/tools/operations/table.js +15 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,6 +20,14 @@ Whether you want to give Claude superpowers over your knowledge base or just wan
|
|
|
20
20
|
|
|
21
21
|

|
|
22
22
|
|
|
23
|
+
## What's New in v6.3
|
|
24
|
+
|
|
25
|
+
**The CLI and the MCP tools agree on tables, headings and nesting.** A `table` in `roam batch` was laid out column by column; it now uses the same row-by-row layout as `roam_create_table`. `roam save` nests what follows a heading by the same rule as `roam_import_markdown`, including when the content under the heading is an indented list. In `roam_create_outline`, `heading` and `children_view_type` now land on the item that asked for them at any depth, where a nested item's heading used to end up on a later top-level block.
|
|
26
|
+
|
|
27
|
+
**Text that only looks like markup is stored as you wrote it.** `2 * 3 * 4` used to be stored as `2 __ 3 __ 4`, and `if a == b and c == d` as `if a ^^ b and c ^^ d`, because any two markers on a line were read as emphasis or a highlight. A marker now counts only when it touches the text it wraps: `*word*` is emphasis, `* word *` is not.
|
|
28
|
+
|
|
29
|
+
**Upgrading:** a `table` row in `roam batch` needs a `label` and one cell for each header after the first, or the batch is refused before anything is written. Text that starts with `# `, `## ` or `### ` is now a heading in `roam_create_page` content and in a one-line `roam_import_markdown`. `roam save` of a file with a heading and indented content now nests that content under the heading; `--flatten` keeps the old result. See the [changelog](CHANGELOG.md) for the full list.
|
|
30
|
+
|
|
23
31
|
## What's New in v6.2
|
|
24
32
|
|
|
25
33
|
**`roam batch` does what its help says.** Outline items can be plain strings (`"items": ["Goal 1", "Goal 2"]`) or `{text, level?}` objects. `--dry-run` writes nothing: a page that does not exist yet is listed in the plan, not created. `parent: "daily"` means today's daily page. A page created with `as` can be used as a parent, so the example in `roam batch --help` runs as written.
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { resolveParentRef, generatePlaceholder } from './resolver.js';
|
|
5
5
|
import { sanitizeTagName } from '../../utils/helpers.js';
|
|
6
|
+
import { buildTableActions } from '../../tools/operations/table.js';
|
|
6
7
|
/**
|
|
7
8
|
* Translate a single command to batch actions
|
|
8
9
|
* May return multiple actions (e.g., table, outline)
|
|
@@ -70,6 +71,18 @@ function registerPlaceholder(name, context) {
|
|
|
70
71
|
context.placeholders.set(name, placeholder);
|
|
71
72
|
return placeholder;
|
|
72
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* A name for a block the caller did not name with `as`, unique in this batch.
|
|
76
|
+
*
|
|
77
|
+
* These were stamped with `Date.now()`. Commands are translated in one
|
|
78
|
+
* synchronous pass, so two of a kind got the same stamp and the same names,
|
|
79
|
+
* and the batch created the second command's blocks under the first one's
|
|
80
|
+
* UIDs. A count held by the batch cannot repeat.
|
|
81
|
+
*/
|
|
82
|
+
function internalName(kind, context) {
|
|
83
|
+
context.internalNames += 1;
|
|
84
|
+
return `_${kind}_${context.internalNames}`;
|
|
85
|
+
}
|
|
73
86
|
/**
|
|
74
87
|
* Update level stack for hierarchy tracking
|
|
75
88
|
*/
|
|
@@ -200,60 +213,18 @@ function translateTodo(cmd, context) {
|
|
|
200
213
|
function translateTable(cmd, context) {
|
|
201
214
|
const { params } = cmd;
|
|
202
215
|
const parentUid = getParentUid(params, context);
|
|
203
|
-
|
|
204
|
-
//
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
}
|
|
216
|
-
});
|
|
217
|
-
// Create columns (headers)
|
|
218
|
-
const columnPlaceholders = [];
|
|
219
|
-
for (let i = 0; i < params.headers.length; i++) {
|
|
220
|
-
const colPlaceholder = registerPlaceholder(`_col_${i}_${Date.now()}`, context);
|
|
221
|
-
columnPlaceholders.push(colPlaceholder);
|
|
222
|
-
actions.push({
|
|
223
|
-
action: 'create-block',
|
|
224
|
-
uid: colPlaceholder,
|
|
225
|
-
string: params.headers[i] || ' ',
|
|
226
|
-
location: {
|
|
227
|
-
'parent-uid': tableContainerPlaceholder,
|
|
228
|
-
order: i
|
|
229
|
-
}
|
|
230
|
-
});
|
|
231
|
-
}
|
|
232
|
-
// Create rows under each column
|
|
233
|
-
for (let rowIdx = 0; rowIdx < params.rows.length; rowIdx++) {
|
|
234
|
-
const row = params.rows[rowIdx];
|
|
235
|
-
// First column gets the row label
|
|
236
|
-
actions.push({
|
|
237
|
-
action: 'create-block',
|
|
238
|
-
string: row.label || ' ',
|
|
239
|
-
location: {
|
|
240
|
-
'parent-uid': columnPlaceholders[0],
|
|
241
|
-
order: rowIdx
|
|
242
|
-
}
|
|
243
|
-
});
|
|
244
|
-
// Remaining columns get the cells
|
|
245
|
-
for (let cellIdx = 0; cellIdx < row.cells.length; cellIdx++) {
|
|
246
|
-
actions.push({
|
|
247
|
-
action: 'create-block',
|
|
248
|
-
string: row.cells[cellIdx] || ' ',
|
|
249
|
-
location: {
|
|
250
|
-
'parent-uid': columnPlaceholders[cellIdx + 1],
|
|
251
|
-
order: rowIdx
|
|
252
|
-
}
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
return actions;
|
|
216
|
+
// The layout is roam_create_table's: one builder, so the CLI and the MCP
|
|
217
|
+
// tool cannot disagree about it. This used to hang every cell under its
|
|
218
|
+
// column's header, which Roam reads as a table turned on its side (#40).
|
|
219
|
+
const scope = internalName('table', context);
|
|
220
|
+
const tableName = params.as ?? scope;
|
|
221
|
+
registerPlaceholder(tableName, context);
|
|
222
|
+
return buildTableActions({
|
|
223
|
+
parent_uid: parentUid,
|
|
224
|
+
order: params.order,
|
|
225
|
+
headers: params.headers,
|
|
226
|
+
rows: params.rows
|
|
227
|
+
}, { table: tableName, cellPrefix: `${scope}_` });
|
|
257
228
|
}
|
|
258
229
|
/**
|
|
259
230
|
* Check an outline's `items` and bring each one to `{text, level, ...}`.
|
|
@@ -318,7 +289,7 @@ function translateOutline(cmd, context) {
|
|
|
318
289
|
}
|
|
319
290
|
};
|
|
320
291
|
// Register placeholder
|
|
321
|
-
const placeholderName = item.as ||
|
|
292
|
+
const placeholderName = item.as || internalName('outline', context);
|
|
322
293
|
action.uid = registerPlaceholder(placeholderName, context);
|
|
323
294
|
if (item.heading) {
|
|
324
295
|
action.heading = item.heading;
|
|
@@ -372,8 +343,9 @@ function translatePage(cmd, context) {
|
|
|
372
343
|
function translateCodeblock(cmd, context) {
|
|
373
344
|
const { params } = cmd;
|
|
374
345
|
const parentUid = getParentUid(params, context);
|
|
375
|
-
// Format code with triple backticks
|
|
376
|
-
|
|
346
|
+
// Format code with triple backticks. `language` is optional: left out, it
|
|
347
|
+
// used to be written into the fence as the word "undefined".
|
|
348
|
+
const codeContent = '```' + (params.language ?? '') + '\n' + params.code + '\n```';
|
|
377
349
|
const action = {
|
|
378
350
|
action: 'create-block',
|
|
379
351
|
string: codeContent,
|
|
@@ -7,6 +7,7 @@ import { resolveGraph } from '../utils/graph.js';
|
|
|
7
7
|
import { collectPageTitles, resolveAllPages, resolveDailyPageUid, needsDailyPage, createResolutionContext, getDailyPageTitle, pageStandIn } from '../batch/resolver.js';
|
|
8
8
|
import { translateAllCommands, normalizeOutlineItems, bindPagePlaceholders } from '../batch/translator.js';
|
|
9
9
|
import { readStdin } from '../utils/input.js';
|
|
10
|
+
import { validateTableInput } from '../../tools/operations/table.js';
|
|
10
11
|
// Required params per command type
|
|
11
12
|
const REQUIRED_PARAMS = {
|
|
12
13
|
todo: ['text'],
|
|
@@ -59,6 +60,19 @@ function validateCommands(commands) {
|
|
|
59
60
|
throw new Error(`[${i}] outline: ${err instanceof Error ? err.message : String(err)}`);
|
|
60
61
|
}
|
|
61
62
|
}
|
|
63
|
+
// A table is checked the way roam_create_table checks it, so the two
|
|
64
|
+
// refuse the same tables, and here rather than partway through a write.
|
|
65
|
+
if (cmdType === 'table') {
|
|
66
|
+
const check = validateTableInput({
|
|
67
|
+
parent_uid: String(params.parent),
|
|
68
|
+
headers: params.headers,
|
|
69
|
+
rows: params.rows
|
|
70
|
+
});
|
|
71
|
+
if (!check.valid) {
|
|
72
|
+
const problems = check.errors.map(e => `${e.field}: ${e.message}`).join('; ');
|
|
73
|
+
throw new Error(`[${i}] table: ${problems}`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
62
76
|
validated.push(cmd);
|
|
63
77
|
}
|
|
64
78
|
return validated;
|
|
@@ -4,7 +4,7 @@ import { basename } from 'path';
|
|
|
4
4
|
import { PageOperations } from '../../tools/operations/pages.js';
|
|
5
5
|
import { TodoOperations } from '../../tools/operations/todos.js';
|
|
6
6
|
import { BatchOperations } from '../../tools/operations/batch.js';
|
|
7
|
-
import { parseMarkdown, generateBlockUid, parseMarkdownHeadingLevel } from '../../markdown-utils.js';
|
|
7
|
+
import { parseMarkdown, nestUnderHeadings, generateBlockUid, parseMarkdownHeadingLevel } from '../../markdown-utils.js';
|
|
8
8
|
import { printDebug, exitWithError } from '../utils/output.js';
|
|
9
9
|
import { resolveGraph } from '../utils/graph.js';
|
|
10
10
|
import { readStdin } from '../utils/input.js';
|
|
@@ -31,55 +31,6 @@ function flattenNodes(nodes, baseLevel = 1) {
|
|
|
31
31
|
}
|
|
32
32
|
return result;
|
|
33
33
|
}
|
|
34
|
-
/**
|
|
35
|
-
* Infer hierarchy from heading levels when all blocks are at the same level.
|
|
36
|
-
* This handles prose-style markdown where headings (# ## ###) define structure
|
|
37
|
-
* without explicit indentation.
|
|
38
|
-
*
|
|
39
|
-
* Example: "# Title\n## Chapter\nContent" becomes:
|
|
40
|
-
* - Title (level 1, H1)
|
|
41
|
-
* - Chapter (level 2, H2)
|
|
42
|
-
* - Content (level 3)
|
|
43
|
-
*/
|
|
44
|
-
function adjustLevelsForHeadingHierarchy(blocks) {
|
|
45
|
-
if (blocks.length === 0)
|
|
46
|
-
return blocks;
|
|
47
|
-
// Only apply heading-based adjustment when:
|
|
48
|
-
// 1. All blocks are at the same level (no indentation-based hierarchy)
|
|
49
|
-
// 2. There are headings present
|
|
50
|
-
const allSameLevel = blocks.every(b => b.level === blocks[0].level);
|
|
51
|
-
const hasHeadings = blocks.some(b => b.heading);
|
|
52
|
-
if (!allSameLevel || !hasHeadings) {
|
|
53
|
-
// Indentation-based hierarchy exists, preserve it
|
|
54
|
-
return blocks;
|
|
55
|
-
}
|
|
56
|
-
const result = [];
|
|
57
|
-
// Track heading stack: each entry is { headingLevel: 1|2|3, adjustedLevel: number }
|
|
58
|
-
const headingStack = [];
|
|
59
|
-
for (const block of blocks) {
|
|
60
|
-
if (block.heading) {
|
|
61
|
-
// Pop headings of same or lower priority (higher h-number)
|
|
62
|
-
while (headingStack.length > 0 &&
|
|
63
|
-
headingStack[headingStack.length - 1].headingLevel >= block.heading) {
|
|
64
|
-
headingStack.pop();
|
|
65
|
-
}
|
|
66
|
-
// New heading level is one deeper than parent heading (or 1 if no parent)
|
|
67
|
-
const adjustedLevel = headingStack.length > 0
|
|
68
|
-
? headingStack[headingStack.length - 1].adjustedLevel + 1
|
|
69
|
-
: 1;
|
|
70
|
-
headingStack.push({ headingLevel: block.heading, adjustedLevel });
|
|
71
|
-
result.push({ ...block, level: adjustedLevel });
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
// Content: nest under current heading context
|
|
75
|
-
const adjustedLevel = headingStack.length > 0
|
|
76
|
-
? headingStack[headingStack.length - 1].adjustedLevel + 1
|
|
77
|
-
: 1;
|
|
78
|
-
result.push({ ...block, level: adjustedLevel });
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
return result;
|
|
82
|
-
}
|
|
83
34
|
/**
|
|
84
35
|
* Check if content looks like a JSON array
|
|
85
36
|
*/
|
|
@@ -338,9 +289,11 @@ JSON format (--json):
|
|
|
338
289
|
else if (isFile || content.includes('\n')) {
|
|
339
290
|
// Multi-line content: parse as markdown
|
|
340
291
|
const nodes = parseMarkdown(content);
|
|
341
|
-
|
|
342
|
-
//
|
|
343
|
-
|
|
292
|
+
// What follows a heading goes under it, unless --flatten asks for
|
|
293
|
+
// only the nesting the indentation gives. This is the rule
|
|
294
|
+
// roam_import_markdown uses. `save` had one of its own, which gave
|
|
295
|
+
// up as soon as any line in the file was indented.
|
|
296
|
+
contentBlocks = flattenNodes(options.flatten ? nodes : nestUnderHeadings(nodes));
|
|
344
297
|
}
|
|
345
298
|
else {
|
|
346
299
|
// Single line text: detect heading syntax and strip hashes
|
package/build/markdown-utils.js
CHANGED
|
@@ -82,13 +82,21 @@ function convertToRoamMarkdown(text) {
|
|
|
82
82
|
// Handle double asterisks/underscores (bold)
|
|
83
83
|
text = text.replace(/\*\*(.+?)\*\*/g, '**$1**'); // Preserve double asterisks
|
|
84
84
|
// Handle single asterisks/underscores (italic)
|
|
85
|
-
|
|
85
|
+
//
|
|
86
|
+
// A marker only opens emphasis when the text it wraps starts right after it,
|
|
87
|
+
// and only closes it when that text ends right before it: "*word*", never
|
|
88
|
+
// "* word *". Without that rule any two markers on a line were a pair, so
|
|
89
|
+
// "2 * 3 * 4" became "2 __ 3 __ 4", and a star bullet's own asterisk was
|
|
90
|
+
// paired with the emphasis later on its line. The same holds for highlights
|
|
91
|
+
// below ("if a == b and c == d").
|
|
92
|
+
text = text.replace(/(?<!\*)\*(?![\s*])(.+?)(?<![\s*])\*(?!\*)/g, '__$1__'); // Single asterisk to double underscore
|
|
86
93
|
// Single underscore to italic, but only at word boundaries. Underscores flanked by
|
|
87
94
|
// word chars (\w includes _) are literal per CommonMark, so snake_case filenames and
|
|
88
95
|
// URLs like /wiki/Ning_Li_(physicist) are left intact instead of becoming __Li__.
|
|
89
|
-
text = text.replace(/(?<!\w)_(?!
|
|
90
|
-
// Handle highlights
|
|
91
|
-
|
|
96
|
+
text = text.replace(/(?<!\w)_(?![\s_])(.+?)(?<!\s)_(?!\w)/g, '__$1__');
|
|
97
|
+
// Handle highlights. Exactly two equals signs make a marker: a longer run is
|
|
98
|
+
// a rule ("=====") or a comparison ("a === b"), not a highlight.
|
|
99
|
+
text = text.replace(/(?<!=)==(?![\s=])(.+?)(?<![\s=])==(?!=)/g, '^^$1^^');
|
|
92
100
|
// Convert tasks
|
|
93
101
|
text = text.replace(/- \[ \]/g, '- {{[[TODO]]}}');
|
|
94
102
|
text = text.replace(/- \[x\]/g, '- {{[[DONE]]}}');
|
|
@@ -4,7 +4,7 @@ import { McpError, ErrorCode } from '@modelcontextprotocol/sdk/types.js';
|
|
|
4
4
|
import { formatRoamDate } from '../../utils/helpers.js';
|
|
5
5
|
import { findOrCreatePage, getPageUid, getOrCreateTodayPage } from '../helpers/page-resolution.js';
|
|
6
6
|
import { executeBatch } from '../helpers/batch-utils.js';
|
|
7
|
-
import { parseMarkdown, nestUnderHeadings, convertToRoamActions, convertToRoamActionsWithBlocks, convertToRoamMarkdown } from '../../markdown-utils.js';
|
|
7
|
+
import { parseMarkdown, nestUnderHeadings, convertToRoamActions, convertToRoamActionsWithBlocks, convertToRoamMarkdown, parseMarkdownHeadingLevel } from '../../markdown-utils.js';
|
|
8
8
|
import { executeStagedBatch } from '../../shared/staged-batch.js';
|
|
9
9
|
import { ednString } from '../../shared/datalog.js';
|
|
10
10
|
// Threshold for skipping child fetch during verification
|
|
@@ -23,6 +23,10 @@ function blockInfoToNestedBlocks(blocks, level = 0) {
|
|
|
23
23
|
children: block.children.length > 0 ? blockInfoToNestedBlocks(block.children, level + 1) : undefined
|
|
24
24
|
}));
|
|
25
25
|
}
|
|
26
|
+
/** A parsed markdown tree as a flat list, parents before their children. */
|
|
27
|
+
function inDocumentOrder(nodes) {
|
|
28
|
+
return nodes.flatMap(node => [node, ...inDocumentOrder(node.children)]);
|
|
29
|
+
}
|
|
26
30
|
export class OutlineOperations {
|
|
27
31
|
constructor(graph) {
|
|
28
32
|
this.graph = graph;
|
|
@@ -243,30 +247,45 @@ export class OutlineOperations {
|
|
|
243
247
|
}
|
|
244
248
|
prevLevel = item.level;
|
|
245
249
|
}
|
|
246
|
-
// Convert outline items to markdown-like structure
|
|
247
|
-
const
|
|
248
|
-
.map(item => {
|
|
250
|
+
// Convert outline items to markdown-like structure, one chunk per item
|
|
251
|
+
const chunks = validOutline.map(item => {
|
|
249
252
|
const indent = ' '.repeat(item.level - 1);
|
|
250
253
|
// If the item text starts with a markdown heading (e.g., #, ##, ###),
|
|
251
254
|
// treat it as a direct heading without adding a bullet or outline indentation.
|
|
252
255
|
// NEW CHANGE: Handle standalone code blocks - do not prepend bullet
|
|
253
256
|
const isCodeBlock = item.text?.startsWith('```') && item.text.endsWith('```') && item.text.includes('\n');
|
|
254
257
|
return isCodeBlock ? `${indent}${item.text?.trim()}` : `${indent}- ${item.text?.trim()}`;
|
|
255
|
-
})
|
|
256
|
-
.join('\n');
|
|
258
|
+
});
|
|
257
259
|
// Convert to Roam markdown format
|
|
258
|
-
const convertedContent = convertToRoamMarkdown(
|
|
260
|
+
const convertedContent = convertToRoamMarkdown(chunks.join('\n'));
|
|
259
261
|
// Parse markdown into hierarchical structure
|
|
260
|
-
|
|
261
|
-
//
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
262
|
+
const nodes = parseMarkdown(convertedContent);
|
|
263
|
+
// Give each item's `heading` and `children_view_type` to the block that
|
|
264
|
+
// item became. The parser returns a tree, in the order the lines were
|
|
265
|
+
// written, so walking it depth-first meets the blocks in item order; an
|
|
266
|
+
// item is one block unless its text spans several lines, and parsing its
|
|
267
|
+
// chunk alone says how many. These used to be copied by position onto the
|
|
268
|
+
// top-level blocks only, which put a nested item's settings on a later
|
|
269
|
+
// top-level block and gave nested blocks none.
|
|
270
|
+
const blocksInOrder = inDocumentOrder(nodes);
|
|
271
|
+
const firstBlockOf = [];
|
|
272
|
+
let nextBlock = 0;
|
|
273
|
+
for (const chunk of chunks) {
|
|
274
|
+
firstBlockOf.push(nextBlock);
|
|
275
|
+
nextBlock += inDocumentOrder(parseMarkdown(chunk)).length;
|
|
276
|
+
}
|
|
277
|
+
// The counts can disagree when a code fence left open in one item takes
|
|
278
|
+
// in the items after it. No block can be traced to its item then, so
|
|
279
|
+
// none is given settings that may belong to another.
|
|
280
|
+
if (nextBlock === blocksInOrder.length) {
|
|
281
|
+
validOutline.forEach((item, index) => {
|
|
282
|
+
const node = blocksInOrder[firstBlockOf[index]];
|
|
283
|
+
if (item.heading)
|
|
284
|
+
node.heading_level = item.heading;
|
|
285
|
+
if (item.children_view_type)
|
|
286
|
+
node.children_view_type = item.children_view_type;
|
|
287
|
+
});
|
|
288
|
+
}
|
|
270
289
|
// Convert nodes to batch actions (flat list)
|
|
271
290
|
const resolvedOrder = order !== undefined ? order : 'last';
|
|
272
291
|
const actions = convertToRoamActions(nodes, targetParentUid, resolvedOrder);
|
|
@@ -396,23 +415,27 @@ export class OutlineOperations {
|
|
|
396
415
|
};
|
|
397
416
|
}
|
|
398
417
|
else {
|
|
399
|
-
// Create a simple block for non-nested content
|
|
418
|
+
// Create a simple block for non-nested content. One line can still be a
|
|
419
|
+
// heading: "## Title" is one in longer content, and was written here as
|
|
420
|
+
// typed, hashes and all. Anything else is still written as typed.
|
|
421
|
+
const { heading_level, content: headingText } = parseMarkdownHeadingLevel(content);
|
|
422
|
+
const text = heading_level > 0 ? headingText : content;
|
|
400
423
|
await executeBatch(this.graph, [{
|
|
401
424
|
action: 'create-block',
|
|
402
425
|
location: {
|
|
403
426
|
"parent-uid": targetParentUid,
|
|
404
427
|
"order": order
|
|
405
428
|
},
|
|
406
|
-
block: { string:
|
|
429
|
+
block: { string: text, ...(heading_level > 0 && { heading: heading_level }) }
|
|
407
430
|
}], 'create content block');
|
|
408
431
|
// For single-line content, we still need to fetch the UID and construct a NestedBlock
|
|
409
432
|
const createdBlocks = [];
|
|
410
433
|
try {
|
|
411
|
-
const foundUid = await this.findBlockWithRetry(targetParentUid,
|
|
434
|
+
const foundUid = await this.findBlockWithRetry(targetParentUid, text);
|
|
412
435
|
if (foundUid) {
|
|
413
436
|
createdBlocks.push({
|
|
414
437
|
uid: foundUid,
|
|
415
|
-
text
|
|
438
|
+
text,
|
|
416
439
|
level: 0,
|
|
417
440
|
order: 0,
|
|
418
441
|
children: []
|
|
@@ -229,13 +229,21 @@ export class PageOperations {
|
|
|
229
229
|
}
|
|
230
230
|
}
|
|
231
231
|
// Convert to node format with level info
|
|
232
|
-
const nodes = normalizedContent.map(block =>
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
232
|
+
const nodes = normalizedContent.map(block => {
|
|
233
|
+
// One to three hashes at the start of the text are a heading, as
|
|
234
|
+
// they are wherever else block text is written; `heading` decides
|
|
235
|
+
// when both are given. The hashes used to be removed and the
|
|
236
|
+
// heading with them, leaving a plain block.
|
|
237
|
+
const hashes = block.text.match(/^(#{1,3})\s+/);
|
|
238
|
+
const heading = block.heading || hashes?.[1].length;
|
|
239
|
+
return {
|
|
240
|
+
content: convertToRoamMarkdown(block.text.replace(/^#+\s+/, '')),
|
|
241
|
+
level: block.level,
|
|
242
|
+
...(heading && { heading_level: heading }),
|
|
243
|
+
...(block.numbered_children && { children_view_type: 'numbered' }),
|
|
244
|
+
children: []
|
|
245
|
+
};
|
|
246
|
+
});
|
|
239
247
|
// Create hierarchical structure based on levels
|
|
240
248
|
const rootNodes = [];
|
|
241
249
|
const levelMap = {};
|
|
@@ -42,6 +42,7 @@ export function validateTableInput(input) {
|
|
|
42
42
|
}
|
|
43
43
|
return { valid: errors.length === 0, errors };
|
|
44
44
|
}
|
|
45
|
+
const SINGLE_TABLE_NAMES = { table: 'table', cellPrefix: '' };
|
|
45
46
|
/**
|
|
46
47
|
* Builds batch actions for creating a Roam table structure.
|
|
47
48
|
*
|
|
@@ -49,21 +50,26 @@ export function validateTableInput(input) {
|
|
|
49
50
|
* - The table container block contains {{[[table]]}}
|
|
50
51
|
* - Header row is nested deeply (each column nested under the previous)
|
|
51
52
|
* - Data rows follow the same nesting pattern
|
|
53
|
+
*
|
|
54
|
+
* This is the one place a table's layout is decided: `roam_create_table`,
|
|
55
|
+
* tables in `roam_create_page` content and `roam batch` all build through it.
|
|
52
56
|
*/
|
|
53
|
-
export function buildTableActions(input) {
|
|
57
|
+
export function buildTableActions(input, names = SINGLE_TABLE_NAMES) {
|
|
54
58
|
const actions = [];
|
|
59
|
+
const tableUid = `{{uid:${names.table}}}`;
|
|
60
|
+
const cellUid = (name) => `{{uid:${names.cellPrefix}${name}}}`;
|
|
55
61
|
// Create table container
|
|
56
62
|
actions.push({
|
|
57
63
|
action: 'create-block',
|
|
58
|
-
uid:
|
|
64
|
+
uid: tableUid,
|
|
59
65
|
string: '{{[[table]]}}',
|
|
60
66
|
location: { 'parent-uid': input.parent_uid, order: input.order ?? 'last' }
|
|
61
67
|
});
|
|
62
68
|
// Create header row with nested structure
|
|
63
69
|
// In Roam tables, each column is nested under the previous column
|
|
64
|
-
let headerParent =
|
|
70
|
+
let headerParent = tableUid;
|
|
65
71
|
for (let i = 0; i < input.headers.length; i++) {
|
|
66
|
-
const uid = `
|
|
72
|
+
const uid = cellUid(`header_${i}`);
|
|
67
73
|
const headerText = input.headers[i] || ' '; // Convert empty to space
|
|
68
74
|
actions.push({
|
|
69
75
|
action: 'create-block',
|
|
@@ -78,26 +84,26 @@ export function buildTableActions(input) {
|
|
|
78
84
|
for (let rowIdx = 0; rowIdx < input.rows.length; rowIdx++) {
|
|
79
85
|
const row = input.rows[rowIdx];
|
|
80
86
|
// Row label (first column) - child of table at position rowIdx + 1 (after header)
|
|
81
|
-
const labelUid = `
|
|
87
|
+
const labelUid = cellUid(`row_${rowIdx}_label`);
|
|
82
88
|
const labelText = row.label || ' '; // Convert empty to space
|
|
83
89
|
actions.push({
|
|
84
90
|
action: 'create-block',
|
|
85
91
|
uid: labelUid,
|
|
86
92
|
string: labelText,
|
|
87
|
-
location: { 'parent-uid':
|
|
93
|
+
location: { 'parent-uid': tableUid, order: rowIdx + 1 }
|
|
88
94
|
});
|
|
89
95
|
// Row cells - each cell is nested under the previous
|
|
90
96
|
let cellParent = labelUid;
|
|
91
97
|
for (let cellIdx = 0; cellIdx < row.cells.length; cellIdx++) {
|
|
92
|
-
const
|
|
98
|
+
const uid = cellUid(`row_${rowIdx}_cell_${cellIdx}`);
|
|
93
99
|
const cellText = row.cells[cellIdx] || ' '; // Convert empty to space
|
|
94
100
|
actions.push({
|
|
95
101
|
action: 'create-block',
|
|
96
|
-
uid
|
|
102
|
+
uid,
|
|
97
103
|
string: cellText,
|
|
98
104
|
location: { 'parent-uid': cellParent, order: 0 }
|
|
99
105
|
});
|
|
100
|
-
cellParent =
|
|
106
|
+
cellParent = uid;
|
|
101
107
|
}
|
|
102
108
|
}
|
|
103
109
|
return actions;
|