Skip to content

Commit

Permalink
update parser organization and names (verb + subject; parse_blocks li…
Browse files Browse the repository at this point in the history
…ke cluster_sources)
  • Loading branch information
Brian Joseph Petro committed Dec 25, 2024
1 parent eaebc12 commit c5f6644
Show file tree
Hide file tree
Showing 13 changed files with 29 additions and 30 deletions.
1 change: 0 additions & 1 deletion smart-blocks/adapters/markdown_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/

import { BlockContentAdapter } from "./_adapter.js";
import { markdown_to_blocks } from "../../smart-sources/blocks/markdown_to_blocks.js";

/**
* @class MarkdownBlockContentAdapter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { markdown_to_blocks } from "./markdown_to_blocks.js";
import { parse_blocks } from "./markdown.js";

/**
* Reads the content of a specific block from the full markdown content.
Expand All @@ -9,7 +9,7 @@ import { markdown_to_blocks } from "./markdown_to_blocks.js";
* @throws {Error} - If the block_key does not exist.
*/
export function block_read(content, block_key) {
const blocks = markdown_to_blocks(content);
const blocks = parse_blocks(content);
const block_range = blocks[block_key];

if (!block_range) {
Expand All @@ -32,7 +32,7 @@ export function block_read(content, block_key) {
* @throws {Error} - If the block_key does not exist.
*/
export function block_update(content, block_key, new_block_content) {
const blocks = markdown_to_blocks(content);
const blocks = parse_blocks(content);
const block_range = blocks[block_key];

if (!block_range) {
Expand Down Expand Up @@ -60,7 +60,7 @@ export function block_update(content, block_key, new_block_content) {
* @throws {Error} - If the block_key does not exist.
*/
export function block_destroy(content, block_key) {
const blocks = markdown_to_blocks(content);
const blocks = parse_blocks(content);
const block_range = blocks[block_key];

if (!block_range) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function markdown_to_blocks(markdown) {
export function parse_blocks(markdown) {
const lines = markdown.split('\n');
const result = {};
const heading_stack = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import { markdown_to_blocks } from "./markdown_to_blocks.js";
import { parse_blocks } from "./markdown.js";

test('convert markdown with complex heading structures to flat JS object', t => {
const markdown = `# Top-Level Heading
Expand Down Expand Up @@ -87,7 +87,7 @@ In this case, \`#Another Top-Level Heading[2]\`. No \`#\` separates the heading
"#Another Top-Level Heading[2]#{1}": [53, 55]
};

const result = markdown_to_blocks(markdown);
const result = parse_blocks(markdown);
t.deepEqual(result, expected);
});

Expand All @@ -111,7 +111,7 @@ Thank you for reading.`;
"#Conclusion#{1}": [8, 8]
};

const result = markdown_to_blocks(markdown);
const result = parse_blocks(markdown);
t.deepEqual(result, expected);
});

Expand Down Expand Up @@ -151,7 +151,7 @@ test('convert markdown with deeply nested headings to flat JS object', t => {
"#Chapter 3": [19, 20]
};

const result = markdown_to_blocks(markdown);
const result = parse_blocks(markdown);
t.deepEqual(result, expected);
});

Expand Down Expand Up @@ -185,7 +185,7 @@ Overview detailed information.
"#Overview[3]#Details#{1}": [14, 15]
};

const result = markdown_to_blocks(markdown);
const result = parse_blocks(markdown);
t.deepEqual(result, expected);
});

Expand Down Expand Up @@ -232,7 +232,7 @@ Final thoughts.`;
"#Conclusion#{1}": [24, 24]
};

const result = markdown_to_blocks(markdown);
const result = parse_blocks(markdown);
t.deepEqual(result, expected);
});

Expand All @@ -258,7 +258,7 @@ Content under second occurrence of top-level heading.
"#Heading[2]#{1}": [10, 11]
};

const result = markdown_to_blocks(markdown);
const result = parse_blocks(markdown);
t.deepEqual(result, expected);
});

Expand All @@ -275,7 +275,7 @@ Content under heading one
"#Heading One#{1}": [4, 5]
};

const result = markdown_to_blocks(markdown);
const result = parse_blocks(markdown);
t.deepEqual(result, expected);
});

Expand All @@ -298,7 +298,7 @@ Content under heading one
"#Heading One#{1}": [10, 11]
};

const result = markdown_to_blocks(markdown);
const result = parse_blocks(markdown);
t.deepEqual(result, expected);
});

Expand All @@ -318,7 +318,7 @@ Content under heading one
"#Heading One#{1}": [5, 6]
};

const result = markdown_to_blocks(markdown);
const result = parse_blocks(markdown);
t.deepEqual(result, expected);
});

Expand All @@ -341,7 +341,7 @@ Some text
"#Another Heading#{1}": [9, 10]
};

const result = markdown_to_blocks(markdown);
const result = parse_blocks(markdown);
t.deepEqual(result, expected);
});

Expand All @@ -355,7 +355,7 @@ Content under heading.
"#\"Heading\"#{1}": [2, 3]
};

const result = markdown_to_blocks(markdown);
const result = parse_blocks(markdown);
t.deepEqual(result, expected);
});

Expand All @@ -370,7 +370,7 @@ Content under heading.
"#": [1, 5]
};

const result = markdown_to_blocks(markdown);
const result = parse_blocks(markdown);
t.deepEqual(result, expected);
});

Expand Down Expand Up @@ -410,7 +410,7 @@ test('should handle nested list items with line break between items', t => {
"#Heading####New Environment#{2}": [19, 25]
};

const result = markdown_to_blocks(markdown);
const result = parse_blocks(markdown);
t.deepEqual(result, expected);
});

Expand Down Expand Up @@ -448,7 +448,7 @@ Dolor Sit Amet
"###Lorem Ipsum#{8}": [17, 20]
};

const result = markdown_to_blocks(markdown);
const result = parse_blocks(markdown);
t.deepEqual(result, expected);
});

Expand All @@ -474,6 +474,6 @@ Content under second occurrence of top-level heading.
"#####Heading[2]#{1}": [10, 11]
};

const result = markdown_to_blocks(markdown);
const result = parse_blocks(markdown);
t.deepEqual(result, expected);
});
6 changes: 3 additions & 3 deletions smart-blocks/smart_blocks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SmartEntities } from "smart-entities";
import { get_markdown_links } from "smart-sources/utils/get_markdown_links.js";
import { get_line_range } from "smart-sources/utils/get_line_range.js";
import { markdown_to_blocks } from "smart-sources/blocks/markdown_to_blocks.js";
import { parse_blocks } from "smart-blocks/parsers/markdown.js";

/**
* @class SmartBlocks
Expand All @@ -18,14 +18,14 @@ export class SmartBlocks extends SmartEntities {
/**
* @method import_source
* @description Imports blocks for a given source by parsing the content. Delegates parsing to a parser
* depending on the source.file_type (e.g., markdown_to_blocks for .md).
* depending on the source.file_type (e.g., parse_blocks for .md).
* @async
* @param {SmartSource} source The source whose blocks are to be imported.
* @param {string} content The raw content of the source file.
* @returns {Promise<void>}
*/
async import_source(source, content) {
let blocks_obj = markdown_to_blocks(content);
let blocks_obj = parse_blocks(content);

const blocks = [];
for (const [sub_key, line_range] of Object.entries(blocks_obj)) {
Expand Down
7 changes: 4 additions & 3 deletions smart-sources/adapters/_file.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SourceContentAdapter } from "./_adapter.js";
import { parse_blocks } from "smart-blocks/parsers/markdown.js";
/**
* @class FileSourceContentAdapter
* @extends SourceContentAdapter
Expand Down Expand Up @@ -136,11 +137,11 @@ export class FileSourceContentAdapter extends SourceContentAdapter {
*/
async merge(content, opts = {}) {
const { mode = 'append_blocks' } = opts;
const blocks_obj = markdown_to_blocks(content);
const blocks_obj = parse_blocks(content);

if (typeof blocks_obj !== 'object' || Array.isArray(blocks_obj)) {
console.warn("merge error: Expected an object from markdown_to_blocks, but received:", blocks_obj);
throw new Error("merge error: markdown_to_blocks did not return an object as expected.");
console.warn("merge error: Expected an object from parse_blocks, but received:", blocks_obj);
throw new Error("merge error: parse_blocks did not return an object as expected.");
}
const { new_blocks, new_with_parent_blocks, changed_blocks, same_blocks } = await this.get_changes(blocks_obj, content);
for(const block of new_blocks){
Expand Down
1 change: 0 additions & 1 deletion smart-sources/adapters/markdown_source.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { FileSourceContentAdapter } from "./_file.js";
import { markdown_to_blocks } from "../blocks/markdown_to_blocks.js";
import { get_markdown_links } from "../utils/get_markdown_links.js";

/**
Expand Down
2 changes: 1 addition & 1 deletion smart-sources/test/test_content.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Content at the deepest heading level.
// Integration test notes:
// - Verify that up to six levels of headings are correctly identified as blocks.
// - Check how block removal or updates propagate when dealing with deeply nested headings.
// - Test that markdown_to_blocks does not break with extreme heading nesting.
// - Test that parse_blocks does not break with extreme heading nesting.
// - Validate that rewriting minimal files after import preserves heading structure.

///////////////////////////////////////////////
Expand Down

0 comments on commit c5f6644

Please sign in to comment.