-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
57 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>`; | ||
}; |
2 changes: 1 addition & 1 deletion
2
templates/components/code.js → templates/components/inline-code.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters