feat: csv front matter
This commit is contained in:
@@ -2,7 +2,7 @@ import { customElement, noShadowDOM } from 'solid-element';
|
||||
import { createSignal, For, Show, createEffect, createMemo, createResource } from 'solid-js';
|
||||
import { marked } from '../markdown';
|
||||
import { resolvePath } from './utils/path';
|
||||
import { loadCSV } from './utils/csv-loader';
|
||||
import {loadCSV, CSV, processVariables} from './utils/csv-loader';
|
||||
|
||||
export interface TableProps {
|
||||
roll?: boolean;
|
||||
@@ -17,7 +17,7 @@ interface TableRow {
|
||||
|
||||
customElement('md-table', { roll: false, remix: false }, (props, { element }) => {
|
||||
noShadowDOM();
|
||||
const [rows, setRows] = createSignal<TableRow[]>([]);
|
||||
const [rows, setRows] = createSignal<CSV<TableRow>>([]);
|
||||
const [activeTab, setActiveTab] = createSignal(0);
|
||||
const [activeGroup, setActiveGroup] = createSignal<string | null>(null);
|
||||
const [bodyHtml, setBodyHtml] = createSignal('');
|
||||
@@ -78,21 +78,8 @@ customElement('md-table', { roll: false, remix: false }, (props, { element }) =>
|
||||
|
||||
// 处理 body 内容中的 {{prop}} 语法并解析 markdown
|
||||
const processBody = (body: string, currentRow: TableRow): string => {
|
||||
let processedBody = body;
|
||||
|
||||
if (!props.remix) {
|
||||
// 不启用 remix 时,只替换当前行的引用
|
||||
processedBody = body.replace(/\{\{(\w+)\}\}/g, (_, key) => currentRow[key] || '');
|
||||
} else {
|
||||
// 启用 remix 时,每次引用使用随机行的内容
|
||||
processedBody = body.replace(/\{\{(\w+)\}\}/g, (_, key) => {
|
||||
const randomRow = rows()[Math.floor(Math.random() * rows().length)];
|
||||
return randomRow?.[key] || '';
|
||||
});
|
||||
}
|
||||
|
||||
// 使用 marked 解析 markdown
|
||||
return marked.parse(processedBody) as string;
|
||||
return marked.parse(processVariables(body, currentRow, rows(), filteredRows(), props.remix)) as string;
|
||||
};
|
||||
|
||||
// 更新 body 内容
|
||||
|
||||
Reference in New Issue
Block a user