Skip to content

Commit f635f9a

Browse files
authored
docs(v2): code block line highlighting (#1904)
* docs(v2): code block line highlighting * misc: update CHANGELOG * misc: respond to review * docs: add line highlighting to the template
1 parent 7714afb commit f635f9a

File tree

20 files changed

+166
-69
lines changed

20 files changed

+166
-69
lines changed

CHANGELOG-2.x.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
- Significantly reduce main bundle size and initial HTML payload on production build. Generated JS files from webpack is also shorter in name.
99
- Refactor dark toggle into a hook.
1010
- Changed the way we read the `USE_SSH` env variable during deployment to be the same as in v1.
11-
- Add highlight specific lines in code blocks.
1211
- Fix accessing `docs/` or `/docs/xxxx` that does not match any existing doc page should return 404 (Not found) page, not blank page.
12+
- 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.
1313

1414
## 2.0.0-alpha.31
1515

packages/docusaurus-init/templates/classic/docs/doc1.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ No language indicated, so no syntax highlighting.
110110
But let's throw in a <b>tag</b>.
111111
```
112112

113+
```js {2}
114+
function highlightMe() {
115+
console.log('This line can be highlighted!');
116+
}
117+
```
118+
113119
---
114120

115121
## Tables

packages/docusaurus-init/templates/classic/src/css/custom.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,10 @@
2121
--ifm-color-primary-lighter: rgb(102, 212, 189);
2222
--ifm-color-primary-lightest: rgb(146, 224, 208);
2323
}
24+
25+
.docusaurus-highlight-code-line {
26+
background-color: rgb(72, 77, 91);
27+
display: block;
28+
margin: 0 calc(-1 * var(--ifm-pre-padding));
29+
padding: 0 var(--ifm-pre-padding);
30+
}

packages/docusaurus-theme-classic/src/theme/CodeBlock/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export default ({children, className: languageClassName, metastring}) => {
3131
const highlightLinesRange = metastring.match(highlightLinesRangeRegex)[1];
3232
highlightLines = rangeParser.parse(highlightLinesRange).filter(n => n > 0);
3333
}
34+
3435
useEffect(() => {
3536
let clipboard;
3637

@@ -73,7 +74,7 @@ export default ({children, className: languageClassName, metastring}) => {
7374
const lineProps = getLineProps({line, key: i});
7475

7576
if (highlightLines.includes(i + 1)) {
76-
lineProps.className = `${lineProps.className} highlight-line`;
77+
lineProps.className = `${lineProps.className} docusaurus-highlight-code-line`;
7778
}
7879

7980
return (

packages/docusaurus-theme-live-codeblock/src/theme/CodeBlock/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export default ({
9292
const lineProps = getLineProps({line, key: i});
9393

9494
if (highlightLines.includes(i + 1)) {
95-
lineProps.className = `${lineProps.className} highlight-line`;
95+
lineProps.className = `${lineProps.className} docusaurus-highlight-code-line`;
9696
}
9797

9898
return (

website/docs/advanced-themes.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ To summarize:
1919

2020
## Writing customized Docusaurus themes
2121

22-
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:
22+
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:
2323

24-
```shell
25-
.
24+
```shell {5-7}
25+
website
2626
├── package.json
2727
└── src
2828
├── index.js
@@ -32,6 +32,7 @@ A Docusaurus theme normally includes an `index.js` file where you hook up to the
3232
```
3333

3434
There are two lifecycle methods that are essential to theme implementation:
35+
3536
- [getThemePath](lifecycle-apis.md#getthemepath)
3637
- [getClientModules](lifecycle-apis.md#getclientmodules)
3738

website/docs/blog.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ The only required field is `title`; however, we provide options to add author in
5656

5757
Use the `<!--truncate-->` marker in your blog post to represent what will be shown as the summary when viewing all published blog posts. Anything above `<!--truncate-->` will be part of the summary. For example:
5858

59-
```md
59+
```md {9}
6060
---
6161
title: Truncation Example
6262
---
@@ -82,7 +82,7 @@ You can run your Docusaurus 2 site without a landing page and instead have your
8282

8383
**Note:** Make sure there's no `index.js` page in `src/pages` or else there will be two files mapping to the same route!
8484

85-
```js
85+
```js {10}
8686
// docusaurus.config.js
8787
module.exports = {
8888
// ...

website/docs/configuration.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ It is recommended to check the [deployment docs](deployment.md) for more informa
3636

3737
### Themes, Plugins, and Presets configurations
3838

39-
40-
4139
_This section is a work in progress. [Welcoming PRs](https://github.com/facebook/docusaurus/issues/1640)._
4240

4341
<!--
@@ -59,7 +57,7 @@ Docusaurus guards `docusaurus.config.js` from unknown fields. To add a custom fi
5957

6058
Example:
6159

62-
```js
60+
```js {3-6}
6361
// docusaurus.config.js
6462
module.exports = {
6563
customFields: {
@@ -75,7 +73,7 @@ Your configuration object will be made available to all the components of your s
7573

7674
Basic Example:
7775

78-
```jsx
76+
```jsx {2,5-6}
7977
import React from 'react';
8078
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
8179

website/docs/deployment.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ You may refer to GitHub Pages' documentation [User, Organization, and Project Pa
5656

5757
Example:
5858

59-
```jsx
59+
```jsx {3-6}
6060
module.exports = {
6161
...
6262
url: 'https://endiliey.github.io', // Your website URL
@@ -102,7 +102,7 @@ References:
102102

103103
To deploy your Docusaurus 2 sites to [Netlify](https://www.netlify.com/), first make sure the following options are properly configured:
104104

105-
```js
105+
```js {3-4}
106106
// docusaurus.config.js
107107
module.exports = {
108108
url: 'https://docusaurus-2.netlify.com', // url to your site with no trailing slash

website/docs/docusaurus-core.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This reusable React component will manage all of your changes to the document he
1111

1212
Usage Example:
1313

14-
```jsx
14+
```jsx {2,6,11}
1515
import React from 'react';
1616
import Head from '@docusaurus/Head';
1717

@@ -29,7 +29,7 @@ const MySEO = () => (
2929

3030
Nested or latter components will override duplicate usages:
3131

32-
```jsx
32+
```jsx {2,5,8,11}
3333
<Parent>
3434
<Head>
3535
<title>My Title</title>
@@ -60,7 +60,7 @@ This component enables linking to internal pages as well as a powerful performan
6060

6161
The component is a wrapper around react-router’s `<NavLink>` component that adds useful enhancements specific to Docusaurus. All props are passed through to react-router’s `<NavLink>` component.
6262

63-
```jsx
63+
```jsx {2,7}
6464
import React from 'react';
6565
import Link from '@docusaurus/Link';
6666

@@ -89,7 +89,7 @@ The target location to navigate to. Example: `/docs/introduction`.
8989

9090
The class to give the `<Link>` when it is active. The default given class is `active`. This will be joined with the `className` prop.
9191

92-
```jsx
92+
```jsx {1}
9393
<Link to="/faq" activeClassName="selected">
9494
FAQs
9595
</Link>
@@ -107,7 +107,7 @@ interface DocusaurusContext {
107107

108108
Usage example:
109109

110-
```jsx
110+
```jsx {2,5}
111111
import React from 'react';
112112
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
113113

@@ -126,7 +126,7 @@ React Hook to automatically append `baseUrl` to a string automatically. This is
126126

127127
Example usage:
128128

129-
```jsx
129+
```jsx {3,11}
130130
import React, {useEffect} from 'react';
131131
import Link from '@docusaurus/Link';
132132
import useBaseUrl from '@docusaurus/useBaseUrl';

0 commit comments

Comments
 (0)