Skip to content
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 8 commits into from Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 28 additions & 25 deletions docs/.eleventy.js
Expand Up @@ -10,7 +10,7 @@ const Image = require("@11ty/eleventy-img");
const path = require("path");
const { slug } = require("github-slugger");
const yaml = require("js-yaml");

const { highlighter, preWrapperPlugin, lineNumberPlugin } = require("./mdSyntaxHighlighter");
const {
DateTime
} = require("luxon");
Expand Down Expand Up @@ -145,7 +145,8 @@ module.exports = function(eleventyConfig) {

eleventyConfig.addPlugin(eleventyNavigationPlugin);
eleventyConfig.addPlugin(syntaxHighlight, {
alwaysWrapLineHighlights: true
alwaysWrapLineHighlights: true,
templateFormats: ["liquid", "njk"]
});
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPlugin(pluginTOC, {
Expand Down Expand Up @@ -186,30 +187,32 @@ module.exports = function(eleventyConfig) {
}

const markdownIt = require("markdown-it");
const md = markdownIt({ html: true, linkify: true, typographer: true, highlight: (str, lang) => highlighter(md, str, lang) })
.use(markdownItAnchor, {
slugify
})
.use(markdownItContainer, "correct", {})
.use(markdownItContainer, "incorrect", {})
.use(markdownItContainer, "warning", {
render(tokens, idx) {
return generateAlertMarkup("warning", tokens, idx);
}
})
.use(markdownItContainer, "tip", {
render(tokens, idx) {
return generateAlertMarkup("tip", tokens, idx);
}
})
.use(markdownItContainer, "important", {
render(tokens, idx) {
return generateAlertMarkup("important", tokens, idx);
}
})
.use(preWrapperPlugin)
.use(lineNumberPlugin)
.disable("code");

eleventyConfig.setLibrary("md",
markdownIt({ html: true, linkify: true, typographer: true })
.use(markdownItAnchor, {
slugify
})
.use(markdownItContainer, "correct", {})
.use(markdownItContainer, "incorrect", {})
.use(markdownItContainer, "warning", {
render(tokens, idx) {
return generateAlertMarkup("warning", tokens, idx);
}
})
.use(markdownItContainer, "tip", {
render(tokens, idx) {
return generateAlertMarkup("tip", tokens, idx);
}
})
.use(markdownItContainer, "important", {
render(tokens, idx) {
return generateAlertMarkup("important", tokens, idx);
}
})
.disable("code"));
eleventyConfig.setLibrary("md", md);

//------------------------------------------------------------------------------
// Shortcodes
Expand Down
81 changes: 81 additions & 0 deletions docs/mdSyntaxHighlighter.js
@@ -0,0 +1,81 @@
/** @typedef {import("markdown-it")} MarkdownIt */
Copy link
Member

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 to md-syntax-highlighter.js?

Also, did you copy any of this code from somewhere else? If so, we need to include the license information.

Copy link
Contributor Author

@chenxsan chenxsan Dec 7, 2022

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.

Copy link
Contributor Author

@chenxsan chenxsan Dec 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LICENSE added.


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;
1 change: 1 addition & 0 deletions docs/package.json
Expand Up @@ -42,6 +42,7 @@
"netlify-cli": "^10.3.1",
"npm-run-all": "^4.1.5",
"postcss-html": "^1.5.0",
"prismjs": "^1.29.0",
"rimraf": "^3.0.2",
"sass": "^1.52.1",
"stylelint": "^14.13.0",
Expand Down
45 changes: 26 additions & 19 deletions docs/src/assets/scss/syntax-highlighter.scss
Expand Up @@ -32,7 +32,7 @@ pre[class*="language-"] {
}

/* Code blocks */
pre[class*="language-"] {
div[class*="language-"] {
padding: 1.5rem;
margin: 1.5rem 0;
overflow: auto;
Expand All @@ -52,8 +52,8 @@ pre[class*="language-"] {

/* Inline code */
:not(pre) > code[class*="language-"] {
padding: .1em;
border-radius: .3em;
padding: 0.1em;
border-radius: 0.3em;
white-space: normal;
}

Expand All @@ -69,7 +69,7 @@ pre[class*="language-"] {
}

.token.namespace {
opacity: .7;
opacity: 0.7;
}

.token.selector,
Expand Down Expand Up @@ -100,25 +100,32 @@ pre[class*="language-"] {
cursor: help;
}

pre {
counter-reset: lineNumber;
}

code .highlight-line {
font-variant-ligatures: none;
.line-numbers-wrapper {
position: absolute;
top: 0;
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);
}

code .highlight-line::before {
-webkit-user-select: none;
.line-number {
user-select: none;
color: var(--icon-color);
content: counter(lineNumber);
counter-increment: lineNumber;
display: inline-block;
font-variant-numeric: tabular-nums;
margin-right: 1.2em;
padding-right: 1.2em;
margin-inline-end: 1.2em;
padding-inline-end: 1.2em;
text-align: right;
width: 2.4em;
width: 1.2em;
}

div[class*="language-"].line-numbers-mode {
position: relative;
}

div[class*="language-"].line-numbers-mode pre[class*="language-"] {
padding-left: calc(2.4em + 1.2rem);
margin-top: 0;
margin-bottom: 0;
chenxsan marked this conversation as resolved.
Show resolved Hide resolved
}