Skip to content

Commit

Permalink
- Extracted markdown block parsing into a separate content parser module
Browse files Browse the repository at this point in the history
- Added new content parsing methods in SmartSource for dynamic parsing
- Introduced content parsers for links and metadata extraction
- Updated README with comprehensive documentation on parsing flow
- Simplified block parsing and import logic across multiple modules
  • Loading branch information
Brian Joseph Petro committed Feb 23, 2025
1 parent 08e6965 commit fe367cb
Show file tree
Hide file tree
Showing 10 changed files with 308 additions and 188 deletions.
57 changes: 57 additions & 0 deletions smart-blocks/content_parsers/parse_markdown_blocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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";
/**
* @method parse_markdown_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);
}
// await Promise.all(blocks);
clean_and_update_source_blocks(source, blocks_obj);
}

/**
* Remove blocks that are no longer present in the parsed block data.
* This ensures that after re-importing a source, stale blocks are cleaned up.
*
* @param {SmartSource} source - The source that was re-imported.
* @param {Object} blocks_obj - The newly parsed blocks object.
*/
function clean_and_update_source_blocks(source, blocks_obj) {
const current_block_keys = new Set(Object.keys(blocks_obj).map(sk => source.key + sk));
const blocks = source.blocks;
for(let i = 0; i < blocks.length; i++){
if(!current_block_keys.has(blocks[i].key)){
blocks[i].deleted = true;
blocks[i].queue_save();
}
}
// Update source data with new blocks
source.data.blocks = blocks_obj;
source.queue_save();
}
59 changes: 0 additions & 59 deletions smart-blocks/smart_blocks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
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 { parse_blocks } from "./parsers/markdown.js";

/**
* @class SmartBlocks
Expand All @@ -15,62 +12,6 @@ export class SmartBlocks extends SmartEntities {
*/
init() { /* mute */ }

/**
* @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., 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 = 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 this.item_type(this.env, block_data);
// blocks.push(this.create_or_update(block_data));
new_item.queue_embed();
this.set(new_item);
}
// await Promise.all(blocks);
this.clean_and_update_source_blocks(source, blocks_obj);
}

/**
* Remove blocks that are no longer present in the parsed block data.
* This ensures that after re-importing a source, stale blocks are cleaned up.
*
* @param {SmartSource} source - The source that was re-imported.
* @param {Object} blocks_obj - The newly parsed blocks object.
*/
clean_and_update_source_blocks(source, blocks_obj) {
const current_block_keys = new Set(Object.keys(blocks_obj).map(sk => source.key + sk));
const blocks = source.blocks;
for(let i = 0; i < blocks.length; i++){
if(!current_block_keys.has(blocks[i].key)){
blocks[i].deleted = true;
blocks[i].queue_save();
}
}
// Update source data with new blocks
source.data.blocks = blocks_obj;
source.queue_save();
}



get fs() { return this.env.smart_sources.fs; }

/**
Expand Down
Loading

0 comments on commit fe367cb

Please sign in to comment.