Skip to content

Commit

Permalink
支持 grid 子选项内容解析
Browse files Browse the repository at this point in the history
  • Loading branch information
qianmoQ committed Feb 7, 2025
1 parent 9095750 commit b12bf5c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
10 changes: 7 additions & 3 deletions docs/content/usage/grid.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ PageForge 支持网格布局语法。
```

::: grid cols-3 gap-6
- __🚀 快速上手__
- ### 🚀 快速上手
零配置,开箱即用,
支持 Markdown 所有基础语法。

Expand All @@ -168,6 +168,10 @@ PageForge 支持网格布局语法。
支持自定义扩展。

- ### 🎨 主题系统
提供多套主题,
支持自定义主题。
提供多套主题,
支持自定义主题。

```java
console.log(1)
```
:::
46 changes: 41 additions & 5 deletions lib/extension/marked/pageforge-grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,61 @@ const PageForgeGridExtension = {
const content = src.slice(headerLength, endIndex);
const raw = src.slice(0, endIndex + 4);

const items = content.split(/\n(?=\s*[-*+]|\s*\d+\.)/g)
.map(item => item.trim())
// 分割列表项并保持原始格式
const items = [];
let currentItem = '';
let inItem = false;

content.split('\n').forEach(line => {
const listMarkerMatch = line.match(/^(\s*)([-*+]|\d+\.)\s/);

if (listMarkerMatch) {
// 如果已经在处理一个项目,保存它
if (currentItem) {
items.push(currentItem.trim());
}
// 开始新的项目,移除列表标记
currentItem = line.slice(listMarkerMatch[0].length);
inItem = true;
}
else if (inItem) {
// 对于项目的后续行,直接添加
currentItem += '\n' + line;
}
});

// 添加最后一个项目
if (currentItem) {
items.push(currentItem.trim());
}

// 为每个项目创建 tokens
const processedItems = items
.filter(item => item)
.map(item => {
return item.replace(/^[-*+]\s+|^\d+\.\s+/, '');
const itemTokens = this.lexer.blockTokens(item, []);
return {
content: item,
tokens: itemTokens
};
});

return {
type: 'pageforgeGrid',
raw,
content: items,
items: processedItems,
options: gridOptions,
tokens: []
};
},

renderer(token) {
const processedItems = token.items.map(item => {
return this.parser.parse(item.tokens);
});

return loadComponent('grid', {
content: token.content,
content: processedItems,
options: token.options,
config: config.feature?.grid?.options || {}
});
Expand Down

0 comments on commit b12bf5c

Please sign in to comment.