diff --git a/CHANGELOG-2.x.md b/CHANGELOG-2.x.md index d6f8436f07cf..188033449c79 100644 --- a/CHANGELOG-2.x.md +++ b/CHANGELOG-2.x.md @@ -8,8 +8,8 @@ - Significantly reduce main bundle size and initial HTML payload on production build. Generated JS files from webpack is also shorter in name. - Refactor dark toggle into a hook. - Changed the way we read the `USE_SSH` env variable during deployment to be the same as in v1. -- Add highlight specific lines in code blocks. - Fix accessing `docs/` or `/docs/xxxx` that does not match any existing doc page should return 404 (Not found) page, not blank page. +- Added code block line highlighting feature (thanks @lex111)! If you have previously swizzled the `CodeBlock` theme component, it is recommended to update your source code to have this feature. ## 2.0.0-alpha.31 diff --git a/packages/docusaurus-init/templates/classic/docs/doc1.md b/packages/docusaurus-init/templates/classic/docs/doc1.md index a350ace5a639..8fddcad47912 100644 --- a/packages/docusaurus-init/templates/classic/docs/doc1.md +++ b/packages/docusaurus-init/templates/classic/docs/doc1.md @@ -110,6 +110,12 @@ No language indicated, so no syntax highlighting. But let's throw in a tag. ``` +```js {2} +function highlightMe() { + console.log('This line can be highlighted!'); +} +``` + --- ## Tables diff --git a/packages/docusaurus-init/templates/classic/src/css/custom.css b/packages/docusaurus-init/templates/classic/src/css/custom.css index a7470eafc7ba..e719d7f09c83 100644 --- a/packages/docusaurus-init/templates/classic/src/css/custom.css +++ b/packages/docusaurus-init/templates/classic/src/css/custom.css @@ -21,3 +21,10 @@ --ifm-color-primary-lighter: rgb(102, 212, 189); --ifm-color-primary-lightest: rgb(146, 224, 208); } + +.docusaurus-highlight-code-line { + background-color: rgb(72, 77, 91); + display: block; + margin: 0 calc(-1 * var(--ifm-pre-padding)); + padding: 0 var(--ifm-pre-padding); +} diff --git a/packages/docusaurus-theme-classic/src/theme/CodeBlock/index.js b/packages/docusaurus-theme-classic/src/theme/CodeBlock/index.js index 1703b57b484c..044710053c8a 100644 --- a/packages/docusaurus-theme-classic/src/theme/CodeBlock/index.js +++ b/packages/docusaurus-theme-classic/src/theme/CodeBlock/index.js @@ -31,6 +31,7 @@ export default ({children, className: languageClassName, metastring}) => { const highlightLinesRange = metastring.match(highlightLinesRangeRegex)[1]; highlightLines = rangeParser.parse(highlightLinesRange).filter(n => n > 0); } + useEffect(() => { let clipboard; @@ -73,7 +74,7 @@ export default ({children, className: languageClassName, metastring}) => { const lineProps = getLineProps({line, key: i}); if (highlightLines.includes(i + 1)) { - lineProps.className = `${lineProps.className} highlight-line`; + lineProps.className = `${lineProps.className} docusaurus-highlight-code-line`; } return ( diff --git a/packages/docusaurus-theme-live-codeblock/src/theme/CodeBlock/index.js b/packages/docusaurus-theme-live-codeblock/src/theme/CodeBlock/index.js index 93b2c083bfc9..097801fb0549 100644 --- a/packages/docusaurus-theme-live-codeblock/src/theme/CodeBlock/index.js +++ b/packages/docusaurus-theme-live-codeblock/src/theme/CodeBlock/index.js @@ -92,7 +92,7 @@ export default ({ const lineProps = getLineProps({line, key: i}); if (highlightLines.includes(i + 1)) { - lineProps.className = `${lineProps.className} highlight-line`; + lineProps.className = `${lineProps.className} docusaurus-highlight-code-line`; } return ( diff --git a/website/docs/advanced-themes.md b/website/docs/advanced-themes.md index fe5f555c66aa..eeef0bb49288 100644 --- a/website/docs/advanced-themes.md +++ b/website/docs/advanced-themes.md @@ -19,10 +19,10 @@ To summarize: ## Writing customized Docusaurus themes -A Docusaurus theme normally includes an `index.js` file where you hook up to the lifecycle methods, alongside with a `theme/` directory of components. A typical Docusaurus theme folder looks like this: +A Docusaurus theme normally includes an `index.js` file where you hook up to the lifecycle methods, alongside with a `theme/` directory of components. A typical Docusaurus `theme` folder looks like this: -```shell -. +```shell {5-7} +website ├── package.json └── src ├── index.js @@ -32,6 +32,7 @@ A Docusaurus theme normally includes an `index.js` file where you hook up to the ``` There are two lifecycle methods that are essential to theme implementation: + - [getThemePath](lifecycle-apis.md#getthemepath) - [getClientModules](lifecycle-apis.md#getclientmodules) diff --git a/website/docs/blog.md b/website/docs/blog.md index 7d37f8bab40d..e211b9b02862 100644 --- a/website/docs/blog.md +++ b/website/docs/blog.md @@ -56,7 +56,7 @@ The only required field is `title`; however, we provide options to add author in Use the `` marker in your blog post to represent what will be shown as the summary when viewing all published blog posts. Anything above `` will be part of the summary. For example: -```md +```md {9} --- title: Truncation Example --- @@ -82,7 +82,7 @@ You can run your Docusaurus 2 site without a landing page and instead have your **Note:** Make sure there's no `index.js` page in `src/pages` or else there will be two files mapping to the same route! -```js +```js {10} // docusaurus.config.js module.exports = { // ... diff --git a/website/docs/configuration.md b/website/docs/configuration.md index 48b4ca0a2e4a..d58bfc3226ca 100644 --- a/website/docs/configuration.md +++ b/website/docs/configuration.md @@ -36,8 +36,6 @@ It is recommended to check the [deployment docs](deployment.md) for more informa ### Themes, Plugins, and Presets configurations - - _This section is a work in progress. [Welcoming PRs](https://github.com/facebook/docusaurus/issues/1640)._