Skip to content

Commit

Permalink
支持 code block 解析
Browse files Browse the repository at this point in the history
  • Loading branch information
qianmoQ committed Dec 29, 2024
1 parent 4010499 commit 3f5f4ca
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pageforge-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ jobs:
run: pnpm run build || echo "No build script found"

test-deploy:
needs: test
needs: test-command
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
1 change: 1 addition & 0 deletions lib/dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class DevServer {
this.config.sourcePath, // 监听整个源文件目录
this.config.templatePath, // 监听整个模板目录
this.config.assetsPath, // 监听资源目录
path.join(__dirname, '..', 'templates'),
path.join(process.cwd(), 'pageforge.yaml')
].filter(Boolean); // 过滤掉 undefined 或 null 的路径

Expand Down
40 changes: 40 additions & 0 deletions lib/extension/marked/pageforge-code-block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const {marked} = require("marked");
const {loadComponent} = require("../../component-loader");

const PageForgeCodeBlockExtension = {
name: 'pageforgeCodeBlock',
level: 'block',
start(src) {
return src.match(/^```/)?.index;
},
tokenizer(src, tokens) {
const rule = /^```(\w*)\n([\s\S]*?)\n```/; // 匹配代码块语法
const match = rule.exec(src);

if (match) {
return {
type: 'pageforgeCodeBlock',
raw: match[0],
lang: match[1], // 语言标识
text: match[2], // 代码内容
tokens: []
};
}
return false;
},
renderer(item) {
return loadComponent('block-code', {
language: item.lang,
text: item.text
});
}
};

marked.use({
extensions: [
PageForgeCodeBlockExtension
],
breaks: true
});

module.exports = PageForgeCodeBlockExtension;
6 changes: 3 additions & 3 deletions lib/extension/marked/pageforge-inline-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const {marked} = require("marked");
const {loadComponent} = require("../../component-loader");

const PageForgeCodeExtension = {
name: 'pageforgeCode',
name: 'pageforgeInlineCode',
level: 'inline',
start(src) {
return src.match(/.*`/)?.index; // 匹配任何以反引号结尾的内容
Expand All @@ -14,7 +14,7 @@ const PageForgeCodeExtension = {

if (match && !src.startsWith('```')) {
return {
type: 'pageforgeCode',
type: 'pageforgeInlineCode',
raw: match[0],
prefix: match[1], // 代码前的文本
text: match[2], // 代码内容
Expand All @@ -25,7 +25,7 @@ const PageForgeCodeExtension = {
return false;
},
renderer(item) {
return item.prefix + loadComponent('code', {text: item.text}) + item.suffix;
return `<span class="inline-block my-1">${item.prefix}${loadComponent('inline-code', {text: item.text})}${item.suffix}</span>`;
}
};

Expand Down
4 changes: 3 additions & 1 deletion lib/extension/marked/pageforge-marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {loadComponent} = require('../../component-loader');
const PageForgeImageExtension = require('./pageforge-image');
const PageForgeLinkExtension = require('./pageforge-link');
const PageForgeInlineCodeExtension = require('./pageforge-inline-code');
const PageForgeCodeBlockExtension = require('./pageforge-code-block');

const renderer = {
paragraph({tokens}) {
Expand Down Expand Up @@ -53,7 +54,8 @@ marked.use({
extensions: [
PageForgeImageExtension,
PageForgeLinkExtension,
PageForgeInlineCodeExtension
PageForgeInlineCodeExtension,
PageForgeCodeBlockExtension
],
renderer,
breaks: false
Expand Down
8 changes: 8 additions & 0 deletions templates/components/block-code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = function template(item) {
const className = "block w-full bg-gray-100 text-gray-900 my-2 px-3 py-2.5 rounded font-mono leading-normal";
const language = item.language ? `language-${item.language}` : '';

const cleanText = item.text.trim();

return `<pre class="${className}"><code class="${language}">${cleanText}</code></pre>`;
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = function template(item) {
return `
<code class="bg-gray-100 text-gray-900 px-1.5 py-0.5 rounded text-sm font-mono inline">
<code class="bg-gray-100 text-gray-900 px-1.5 py-0.5 rounded text-sm font-mono mx-1 inline">
${item.text}
</code>
`;
Expand Down

0 comments on commit 3f5f4ca

Please sign in to comment.