feat: md icon prefix

This commit is contained in:
hypercross 2026-03-13 11:14:01 +08:00
parent 346b97153f
commit 9e8d4e6388
4 changed files with 33 additions and 7 deletions

View File

@ -1,5 +1,5 @@
import {createMemo, For} from 'solid-js'; import {createMemo, For} from 'solid-js';
import { marked } from '../../markdown'; import {parseMarkdown} from '../../markdown';
import { getLayerStyle } from './hooks/dimensions'; import { getLayerStyle } from './hooks/dimensions';
import type { CardData } from './types'; import type { CardData } from './types';
import {DeckStore} from "./hooks/deckStore"; import {DeckStore} from "./hooks/deckStore";
@ -16,7 +16,7 @@ export function CardLayer(props: CardLayerProps) {
const showBounds = () => props.store.state.isEditing; const showBounds = () => props.store.state.isEditing;
function renderLayerContent(content: string) { function renderLayerContent(content: string) {
return marked.parse(processVariables(content, props.cardData, props.store.state.cards)) as string; return parseMarkdown(processVariables(content, props.cardData, props.store.state.cards), props.cardData.iconPath) as string;
} }
return ( return (
<For each={layers()}> <For each={layers()}>

View File

@ -269,7 +269,22 @@ export function createDeckStore(
const generateCode = () => { const generateCode = () => {
const layersStr = formatLayers(state.layerConfigs); const layersStr = formatLayers(state.layerConfigs);
return `:md-deck[${state.rawSrc || state.src}]{size="${state.sizeW}x${state.sizeH}" grid="${state.gridW}x${state.gridH}" bleed="${state.bleed}" padding="${state.padding}" layers="${layersStr}"}`; const parts = [
`:md-deck[${state.rawSrc || state.src}]`,
`{size="${state.sizeW}x${state.sizeH}"`,
`grid="${state.gridW}x${state.gridH}"`
];
// 仅在非默认值时添加 bleed 和 padding
if (state.bleed !== DECK_DEFAULTS.BLEED) {
parts.push(`bleed="${state.bleed}"`);
}
if (state.padding !== DECK_DEFAULTS.PADDING) {
parts.push(`padding="${state.padding}"`);
}
parts.push(`layers="${layersStr}"}`);
return parts.join(' ');
}; };
const copyCode = async () => { const copyCode = async () => {

View File

@ -5,6 +5,15 @@ import markedAlert from "marked-alert";
import markedMermaid from "./mermaid"; import markedMermaid from "./mermaid";
import {gfmHeadingId} from "marked-gfm-heading-id"; import {gfmHeadingId} from "marked-gfm-heading-id";
let globalIconPrefix: string | undefined = undefined;
function overrideIconPrefix(path?: string){
globalIconPrefix = path;
return {
[Symbol.dispose](){
globalIconPrefix = undefined;
}
}
}
// 使用 marked-directive 来支持指令语法 // 使用 marked-directive 来支持指令语法
const marked = new Marked() const marked = new Marked()
.use(gfmHeadingId()) .use(gfmHeadingId())
@ -26,7 +35,8 @@ const marked = new Marked()
// :[blah] becomes <i class="icon icon-blah"></i> // :[blah] becomes <i class="icon icon-blah"></i>
renderer(token) { renderer(token) {
if (!token.meta.name) { if (!token.meta.name) {
return `<icon class="icon-${token.text}"></icon>`; const style = globalIconPrefix ? `style="--icon-src: url('${globalIconPrefix}${token.text}.png')"` : '';
return `<icon ${style} class="icon-${token.text}"></icon>`;
} }
return false; return false;
} }
@ -89,7 +99,8 @@ const marked = new Marked()
}] }]
}); });
export function parseMarkdown(content: string): string { export function parseMarkdown(content: string, iconPrefix?: string): string {
using prefix = overrideIconPrefix(iconPrefix);
return marked.parse(content.trimStart()) as string; return marked.parse(content.trimStart()) as string;
} }

View File

@ -7,7 +7,7 @@ icon{
width: 1em; width: 1em;
height: 1em; height: 1em;
--icon-src: ''; --icon-src: '';
background: var(--icon-src); background-image: var(--icon-src);
background-size: contain; background-size: contain;
background-position: center; background-position: center;
background-repeat: no-repeat; background-repeat: no-repeat;