mindnet_obsidian/src/mapping/sectionParser.ts

111 lines
3.1 KiB
TypeScript
Raw Normal View History

/**
* Section parser: Split markdown by headings and extract wikilinks per section.
*/
export interface NoteSection {
heading: string | null; // null for content before first heading
headingLevel: number; // 0 for content before first heading
content: string; // Full content of section (including heading)
startLine: number; // Line index where section starts
endLine: number; // Line index where section ends (exclusive)
links: string[]; // Deduplicated wikilinks found in this section
}
/**
* Split markdown content into sections by headings.
*/
export function splitIntoSections(markdown: string): NoteSection[] {
const lines = markdown.split(/\r?\n/);
const sections: NoteSection[] = [];
let currentSection: NoteSection | null = null;
let currentContent: string[] = [];
let currentStartLine = 0;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line === undefined) continue;
// Check if line is a heading
const headingMatch = line.match(/^(#{1,6})\s+(.+)$/);
if (headingMatch) {
// Save previous section if exists
if (currentSection !== null || currentContent.length > 0) {
const content = currentContent.join("\n");
const links = extractWikilinks(content);
sections.push({
heading: currentSection?.heading || null,
headingLevel: currentSection?.headingLevel || 0,
content: content,
startLine: currentStartLine,
endLine: i,
links: links,
});
}
// Start new section
const headingLevel = (headingMatch[1]?.length || 0);
const headingText = (headingMatch[2]?.trim() || "");
currentSection = {
heading: headingText,
headingLevel: headingLevel,
content: line,
startLine: i,
endLine: i + 1,
links: [],
};
currentContent = [line];
currentStartLine = i;
} else {
// Add line to current section
if (currentSection) {
currentContent.push(line);
currentSection.content = currentContent.join("\n");
currentSection.endLine = i + 1;
} else {
// Content before first heading
currentContent.push(line);
}
}
}
// Save last section
if (currentSection || currentContent.length > 0) {
const content = currentContent.join("\n");
const links = extractWikilinks(content);
sections.push({
heading: currentSection?.heading || null,
headingLevel: currentSection?.headingLevel || 0,
content: content,
startLine: currentStartLine,
endLine: lines.length,
links: links,
});
}
return sections;
}
/**
* Extract all wikilinks from markdown text (deduplicated).
*/
export function extractWikilinks(markdown: string): string[] {
const links = new Set<string>();
const wikilinkRegex = /\[\[([^\]]+?)\]\]/g;
let match: RegExpExecArray | null;
while ((match = wikilinkRegex.exec(markdown)) !== null) {
if (match[1]) {
const link = match[1].trim();
if (link) {
links.add(link);
}
}
}
return Array.from(links).sort(); // Deterministic ordering
}