New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: optimize code block structure #16669
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ const loadLanguages = require("prismjs/components/"); | |
* @param {MarkdownIt} md markdown-it | ||
* @param {string} str code | ||
* @param {string} lang code language | ||
* @returns | ||
* @returns {string} highlighted result wrapped in pre | ||
*/ | ||
const highlighter = function (md, str, lang) { | ||
let result = ""; | ||
|
@@ -41,29 +41,15 @@ const highlighter = function (md, str, lang) { | |
loadLanguages([lang]); | ||
result = Prism.highlight(str, Prism.languages[lang], lang); | ||
} catch (err) { | ||
console.log(err); | ||
console.log(lang, err); | ||
// we still want to wrap the result later | ||
result = md.utils.escapeHtml(str); | ||
} | ||
} else { | ||
result = md.utils.escapeHtml(str); | ||
} | ||
|
||
return `<pre><code>${result}</code></pre>`; | ||
}; | ||
|
||
/** | ||
* | ||
* modified from https://github.com/vuejs/vitepress/blob/main/src/node/markdown/plugins/preWrapper.ts | ||
* @param {MarkdownIt} md | ||
* @license MIT License. See file header. | ||
*/ | ||
const preWrapperPlugin = (md) => { | ||
const fence = md.renderer.rules.fence; | ||
md.renderer.rules.fence = (...args) => { | ||
const [tokens, idx] = args; | ||
const lang = tokens[idx].info.trim(); | ||
const rawCode = fence(...args); | ||
return `<div class="language-${lang}">${rawCode}</div>`; | ||
}; | ||
return `<pre class="language-${lang}"><code>${result}</code></pre>`; | ||
chenxsan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
/** | ||
|
@@ -93,15 +79,13 @@ const lineNumberPlugin = (md) => { | |
const lineNumbersWrapperCode = `<div class="line-numbers-wrapper" aria-hidden="true">${lineNumbersCode}</div>`; | ||
|
||
const finalCode = rawCode | ||
.replace(/<\/div>$/, `${lineNumbersWrapperCode}</div>`) | ||
.replace(/<\/pre>\n/, `${lineNumbersWrapperCode}</pre>`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
.replace(/"(language-\S*?)"/, '"$1 line-numbers-mode"') | ||
.replace(/<code>/, `<code class="language-${lang}">`) | ||
.replace(/<pre>/, `<pre class="language-${lang}">`); | ||
|
||
return finalCode; | ||
}; | ||
}; | ||
|
||
module.exports.highlighter = highlighter; | ||
module.exports.preWrapperPlugin = preWrapperPlugin; | ||
module.exports.lineNumberPlugin = lineNumberPlugin; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ pre[class*="language-"] { | |
} | ||
|
||
/* Code blocks */ | ||
div[class*="language-"] { | ||
pre[class*="language-"] { | ||
padding: 1.5rem; | ||
margin: 1.5rem 0; | ||
overflow: auto; | ||
|
@@ -43,6 +43,10 @@ div[class*="language-"] { | |
[data-theme="dark"] & { | ||
color: var(--color-neutral-100); | ||
} | ||
|
||
&.line-numbers-mode { | ||
padding-left: calc(1.5rem + 2.4em + 1.2rem); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calculated according to the original implement. |
||
} | ||
} | ||
|
||
:not(pre) > code[class*="language-"], | ||
|
@@ -103,29 +107,21 @@ pre[class*="language-"] { | |
.line-numbers-wrapper { | ||
position: absolute; | ||
top: 0; | ||
left: 1.5rem; | ||
text-align: right; | ||
padding-top: 1.5rem; | ||
font-size: 1em; | ||
font-family: var(--mono-font), Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace; | ||
line-height: 1.5; | ||
color: var(--icon-color); | ||
} | ||
|
||
.line-number { | ||
user-select: none; | ||
color: var(--icon-color); | ||
display: inline-block; | ||
font-variant-numeric: tabular-nums; | ||
text-align: right; | ||
width: 1.2em; | ||
} | ||
|
||
div[class*="language-"].line-numbers-mode { | ||
position: relative; | ||
font-variant-ligatures: none; | ||
|
||
pre[class*="language-"] { | ||
padding-left: calc(2.4em + 1.2rem); | ||
margin-top: 0; | ||
margin-bottom: 0; | ||
.line-number { | ||
user-select: none; | ||
color: var(--icon-color); | ||
display: inline-block; | ||
font-variant-numeric: tabular-nums; | ||
text-align: right; | ||
width: 1.2em; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need wrapper now, so I removed those code.