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: fix broken line numbers #16606
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a6f0e53
docs: fix broken line numbers
chenxsan 9e8d597
docs: fix scss styles
chenxsan c1210e5
docs: fix lint errors
chenxsan e5a478e
Update docs/src/assets/scss/syntax-highlighter.scss
chenxsan 39c801b
docs: fix scss
chenxsan 758d37f
docs: move file and add license
chenxsan 2dde17d
add aria-hidden
chenxsan 90c9415
add license to file header
chenxsan File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** @typedef {import("markdown-it")} MarkdownIt */ | ||
|
||
const Prism = require("prismjs"); | ||
const loadLanguages = require("prismjs/components/"); | ||
|
||
/** | ||
* | ||
* @param {MarkdownIt} md markdown-it | ||
* @param {string} str code | ||
* @param {string} lang code language | ||
* @returns | ||
*/ | ||
const highlighter = function (md, str, lang) { | ||
let result = ""; | ||
if (lang) { | ||
try { | ||
loadLanguages([lang]); | ||
result = Prism.highlight(str, Prism.languages[lang], lang); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
} 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 | ||
*/ | ||
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>`; | ||
}; | ||
}; | ||
|
||
/** | ||
* | ||
* modified from https://github.com/vuejs/vitepress/blob/main/src/node/markdown/plugins/lineNumbers.ts | ||
* @param {MarkdownIt} md | ||
*/ | ||
const lineNumberPlugin = (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); | ||
const code = rawCode.slice( | ||
rawCode.indexOf("<code>"), | ||
rawCode.indexOf("</code>") | ||
); | ||
const lines = code.split("\n"); | ||
const lineNumbersCode = [...Array(lines.length - 1)] | ||
.map( | ||
(line, index) => | ||
`<span class="line-number">${index + 1}</span><br>` | ||
) | ||
.join(""); | ||
|
||
const lineNumbersWrapperCode = `<div class="line-numbers-wrapper">${lineNumbersCode}</div>`; | ||
|
||
const finalCode = rawCode | ||
.replace(/<\/div>$/, `${lineNumbersWrapperCode}</div>`) | ||
.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; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This isn't the right location for this file. Can you please move it to
src/_plugins
and rename tomd-syntax-highlighter.js
?Also, did you copy any of this code from somewhere else? If so, we need to include the license information.
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.
Hi Nicholas, as there're some discrepancies between eslint docs site and vitepress, e.g., they use shiki for syntax highlight and author their code in TypeScript, while we use Prismjs and JavaScript, hence I modified the markdown plugins based on their implementation. So yes, I did copy some code from its repository.
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.
LICENSE
added.