mirror of
https://github.com/runyanjake/cooking.git
synced 2026-03-26 09:53:17 -07:00
46 lines
993 B
TypeScript
46 lines
993 B
TypeScript
export interface RecipeSection {
|
|
title: string;
|
|
content: string;
|
|
}
|
|
|
|
export function parseRecipeSections(markdownContent: string): RecipeSection[] {
|
|
const lines = markdownContent.split('\n');
|
|
const sections: RecipeSection[] = [];
|
|
let currentSection: RecipeSection | null = null;
|
|
let inFrontmatter = false;
|
|
|
|
for (let i = 0; i < lines.length; i++) {
|
|
const line = lines[i];
|
|
|
|
if (line.trim() === '---') {
|
|
inFrontmatter = !inFrontmatter;
|
|
continue;
|
|
}
|
|
|
|
if (inFrontmatter) {
|
|
continue;
|
|
}
|
|
|
|
if (line.startsWith('## ')) {
|
|
if (currentSection) {
|
|
sections.push(currentSection);
|
|
}
|
|
currentSection = {
|
|
title: line.replace('## ', '').trim(),
|
|
content: '',
|
|
};
|
|
} else if (currentSection) {
|
|
currentSection.content += line + '\n';
|
|
}
|
|
}
|
|
|
|
if (currentSection) {
|
|
sections.push(currentSection);
|
|
}
|
|
|
|
return sections.map(section => ({
|
|
...section,
|
|
content: section.content.trim(),
|
|
}));
|
|
}
|