continue-custom/core/config/markdown/parseMarkdownRule.ts
Tomasz Stefaniak a21d350ea1 feat: markdown rules
fix: parsing
2025-05-20 13:12:17 -07:00

68 lines
2 KiB
TypeScript

import { basename } from "path";
import * as YAML from "yaml";
import { RuleWithSource } from "../..";
/**
* Parses markdown content with YAML frontmatter
*/
export function parseMarkdownRule(content: string): {
frontmatter: Record<string, any>;
markdown: string;
} {
// Normalize line endings to \n
const normalizedContent = content.replace(/\r\n/g, "\n");
// More reliable frontmatter detection
const parts = normalizedContent.split(/^---\s*$/m);
// If we have at least 3 parts (before ---, frontmatter, after ---), we have frontmatter
if (parts.length >= 3) {
const frontmatterStr = parts[1];
// Join the remaining parts back together (in case there are more --- in the markdown)
const markdownContent = parts.slice(2).join("---");
try {
// Parse YAML frontmatter
const frontmatter = YAML.parse(frontmatterStr) || {}; // Handle empty frontmatter
return { frontmatter, markdown: markdownContent.trim() };
} catch (e) {
// Error parsing frontmatter, treat as markdown only
console.warn("Error parsing markdown frontmatter:", e);
return { frontmatter: {}, markdown: normalizedContent };
}
}
// No frontmatter found
return { frontmatter: {}, markdown: normalizedContent };
}
/**
* Converts a markdown file with YAML frontmatter to a RuleWithSource object
*/
export function convertMarkdownRuleToContinueRule(
path: string,
content: string,
): RuleWithSource {
const { frontmatter, markdown } = parseMarkdownRule(content);
// Try to extract title from first heading if no name in frontmatter
let name = frontmatter.name;
if (!name) {
// Look for a heading in the markdown
const headingMatch = markdown.match(/^#\s+(.+)$/m);
if (headingMatch) {
name = headingMatch[1].trim();
} else {
// Fall back to filename
name = basename(path).replace(/\.md$/, "");
}
}
return {
rule: markdown,
globs: frontmatter.globs,
name,
source: "rules-block",
ruleFile: path,
};
}