Skip to content

Commit

Permalink
- Updated function name from parse_blocks to `parse_markdown_blocks…
Browse files Browse the repository at this point in the history
…` across multiple modules

- Updated import statements and function calls in markdown-related adapters and parsers
- Maintained consistent naming convention for markdown block parsing function
- Ensured backward compatibility by updating references in test files and utility modules
  • Loading branch information
Brian Joseph Petro committed Feb 23, 2025
1 parent fe367cb commit f259852
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 64 deletions.
8 changes: 4 additions & 4 deletions smart-blocks/adapters/♻️/markdown_crud.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse_blocks } from "./markdown.js";
import { parse_markdown_blocks } from "./markdown.js";

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

if (!block_range) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
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 { parse_blocks } from "../parsers/markdown.js";
import { parse_markdown_blocks } from "../parsers/markdown.js";
/**
* @method parse_markdown_blocks
* @method parse_blocks
* @description Imports blocks for a given source by parsing the content. Delegates parsing to a parser
* 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>}
*/
export async function parse_markdown_source(source, content) {
if(source.file_type !== 'md') return;
let blocks_obj = parse_blocks(content);

for (const [sub_key, line_range] of Object.entries(blocks_obj)) {
// if (sub_key === '#' || sub_key.startsWith('#---frontmatter')) continue;
const block_key = source.key + sub_key;
const block_content = get_line_range(content, line_range[0], line_range[1]);
const block_outlinks = get_markdown_links(block_content);
const block_data = {
key: block_key,
lines: line_range,
size: block_content.length,
outlinks: block_outlinks,
};
// prevent premature save by not using create_or_update
const new_item = new source.block_collection.item_type(source.env, block_data);
// blocks.push(this.create_or_update(block_data));
new_item.queue_embed();
source.block_collection.set(new_item);
export async function parse_blocks(source, content) {
if(source.file_type === 'md') {
let blocks_obj = parse_markdown_blocks(content);
for (const [sub_key, line_range] of Object.entries(blocks_obj)) {
// if (sub_key === '#' || sub_key.startsWith('#---frontmatter')) continue;
const block_key = source.key + sub_key;
const block_content = get_line_range(content, line_range[0], line_range[1]);
const block_outlinks = get_markdown_links(block_content);
const block_data = {
key: block_key,
lines: line_range,
size: block_content.length,
outlinks: block_outlinks,
};
// prevent premature save by not using create_or_update
const new_item = new source.block_collection.item_type(source.env, block_data);
// blocks.push(this.create_or_update(block_data));
new_item.queue_embed();
source.block_collection.set(new_item);
}
// await Promise.all(blocks);
clean_and_update_source_blocks(source, blocks_obj);
}
// await Promise.all(blocks);
clean_and_update_source_blocks(source, blocks_obj);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions smart-blocks/parsers/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* The returned object keys reflect the path of headings (or list items) in the document, and
* each value is an array of two numbers: the starting line and the ending line for that key's content.
*
* @function parse_blocks
* @function parse_markdown_blocks
* @param {string} markdown - The complete Markdown text to parse.
* @param {Object} [opts={}] - Parsing options
* @param {number} [opts.start_index=1] - The line index to treat as line 1
Expand All @@ -26,7 +26,7 @@
* // ...
* }
*/
export function parse_blocks(markdown, opts={}) {
export function parse_markdown_blocks(markdown, opts={}) {
const { start_index = 1, line_keys = false } = opts;
const lines = markdown.split('\n');

Expand Down
38 changes: 19 additions & 19 deletions smart-blocks/parsers/markdown.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import { parse_blocks } from "./markdown.js";
import { parse_markdown_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 = parse_blocks(markdown);
const result = parse_markdown_blocks(markdown);
t.deepEqual(result, expected);
});

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

const result = parse_blocks(markdown);
const result = parse_markdown_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 = parse_blocks(markdown);
const result = parse_markdown_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 = parse_blocks(markdown);
const result = parse_markdown_blocks(markdown);
t.deepEqual(result, expected);
});

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

const result = parse_blocks(markdown);
const result = parse_markdown_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 = parse_blocks(markdown);
const result = parse_markdown_blocks(markdown);
t.deepEqual(result, expected);
});

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

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

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

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

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

const result = parse_blocks(markdown);
const result = parse_markdown_blocks(markdown);
t.deepEqual(result, expected);
});
test('content prior to first heading with multiple nested list items', t => {
Expand All @@ -342,7 +342,7 @@ Content under heading one
"#Heading One#{1}": [10, 11]
};

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

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

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

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

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

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

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

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

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

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

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

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

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

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

const result = parse_blocks(markdown, {start_index: 0});
const result = parse_markdown_blocks(markdown, {start_index: 0});
t.deepEqual(result, expected);
});

Expand All @@ -537,7 +537,7 @@ test('opts.line_keys uses the first three longest words of line in block path ke
* non-sensical list item three has extremely sophisticated words
- sublist item three
`.trim();
const result = parse_blocks(markdown, {line_keys: true});
const result = parse_markdown_blocks(markdown, {line_keys: true});
const expected = {
"#Heading": [1, 7],
"#Heading#longest list item": [2, 3],
Expand Down
4 changes: 2 additions & 2 deletions smart-contexts/utils/respect_exclusions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* until the next heading.
*/

import { parse_blocks } from 'smart-blocks/parsers/markdown.js';
import { parse_markdown_blocks } from 'smart-blocks/parsers/markdown.js';
import { match_glob } from 'smart-file-system/utils/match_glob.js';

/**
Expand Down Expand Up @@ -35,7 +35,7 @@ export async function respect_exclusions(context_snapshot = {}, opts = {}) {
}

export function strip_excluded_headings(content, excluded_list) {
const blocks_map = parse_blocks(content, { start_index: 0 });
const blocks_map = parse_markdown_blocks(content, { start_index: 0 });
if (!Object.keys(blocks_map).length) return [content, []];

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

if (typeof blocks_obj !== 'object' || Array.isArray(blocks_obj)) {
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.");
console.warn("merge error: Expected an object from parse_markdown_blocks, but received:", blocks_obj);
throw new Error("merge error: parse_markdown_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
4 changes: 1 addition & 3 deletions smart-sources/content_parsers/parse_links.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { get_markdown_links } from "../utils/get_markdown_links.js";

export function parse_markdown_links(source, content) {
export function parse_links(source, content) {
if(!source.source_adapter?.get_links) return;
const outlinks = source.source_adapter.get_links(content);
source.data.outlinks = outlinks;
Expand Down
2 changes: 1 addition & 1 deletion smart-sources/content_parsers/parse_metadata.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function parse_markdown_metadata(source, content) {
export function parse_metadata(source, content) {
if(!source.source_adapter?.get_metadata) return;
const {frontmatter} = source.source_adapter?.get_metadata?.(content);
source.data.metadata = frontmatter;
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 parse_blocks does not break with extreme heading nesting.
// - Test that parse_markdown_blocks does not break with extreme heading nesting.
// - Validate that rewriting minimal files after import preserves heading structure.

///////////////////////////////////////////////
Expand Down
8 changes: 4 additions & 4 deletions smart-templates-2/adapters/markdown.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { TemplateSourceAdapter } from './_adapter.js';
import { parse_blocks } from 'smart-blocks/parsers/markdown.js';
import { parse_markdown_blocks } from 'smart-blocks/parsers/markdown.js';

/**
* @class MarkdownTemplateAdapter
* @extends TemplateSourceAdapter
* @description
* Adapter that reads a Markdown file, uses `parse_blocks` to find headings and content,
* Adapter that reads a Markdown file, uses `parse_markdown_blocks` to find headings and content,
* and populates `this.item.data` accordingly.
*/
export class MarkdownTemplateAdapter extends TemplateSourceAdapter {
Expand All @@ -18,13 +18,13 @@ export class MarkdownTemplateAdapter extends TemplateSourceAdapter {
* @method import
* @description
* 1) Read the markdown content
* 2) Parse via `parse_blocks`
* 2) Parse via `parse_markdown_blocks`
* 3) Store heading => content mapping in `this.item.data.headings`
* @returns {Promise<Object>} The headings data object
*/
async import() {
const markdownContent = await this.read();
const blocksMap = parse_blocks(markdownContent);
const blocksMap = parse_markdown_blocks(markdownContent);

const headingsData = {};
for (const [blockKey, [start, end]] of Object.entries(blocksMap)) {
Expand Down

0 comments on commit f259852

Please sign in to comment.