feat: ai's attempt

This commit is contained in:
2026-02-26 14:24:48 +08:00
parent b370e6c302
commit ba7b264a97
6 changed files with 405 additions and 49 deletions
+1 -46
View File
@@ -1,6 +1,6 @@
import { Component, createSignal, onMount, onCleanup, Show } from 'solid-js';
import { parseMarkdown } from '../markdown';
import { fetchData } from '../data-loader';
import { fetchData, extractSection } from '../data-loader';
export interface ArticleProps {
src: string;
@@ -9,51 +9,6 @@ export interface ArticleProps {
onError?: (error: Error) => void;
}
/**
* 从 markdown 内容中提取指定标题下的内容
*/
function extractSection(content: string, sectionTitle: string): string {
// 匹配标题(支持 1-6 级标题)
const sectionRegex = new RegExp(
`^(#{1,6})\\s*${escapeRegExp(sectionTitle)}\\s*$`,
'im'
);
const match = content.match(sectionRegex);
if (!match) {
// 没有找到指定标题,返回空字符串
return '';
}
const headerLevel = match[1].length;
const startIndex = match.index!;
// 查找下一个同级或更高级别的标题
const nextHeaderRegex = new RegExp(
`^#{1,${headerLevel}}\\s+.+$`,
'gm'
);
// 从标题后开始搜索
nextHeaderRegex.lastIndex = startIndex + match[0].length;
const nextMatch = nextHeaderRegex.exec(content);
if (nextMatch) {
// 返回从当前标题到下一个同级/更高级标题之间的内容
return content.slice(startIndex, nextMatch.index).trim();
}
// 返回从当前标题到文件末尾的内容
return content.slice(startIndex).trim();
}
/**
* 转义正则表达式特殊字符
*/
function escapeRegExp(str: string): string {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
/**
* Article 组件
* 用于将特定 src 位置的 md 文件显示为 markdown 文章