|
| 1 | +--- |
| 2 | +date: 2020-06-01T13:31:12+01:00 |
| 3 | +title: Code highlighting |
| 4 | +weight: 16 |
| 5 | +--- |
| 6 | + |
| 7 | +Learn theme uses [highlight.js](https://highlightjs.org/) to provide code syntax highlighting. |
| 8 | + |
| 9 | +## Markdown syntax |
| 10 | + |
| 11 | +Wrap the code block with three backticks and the name of the language. Highlight will try to auto detect the language if one is not provided. |
| 12 | + |
| 13 | +<!-- markdownlint-disable MD046 --> |
| 14 | +```plaintext |
| 15 | + ```json |
| 16 | + [ |
| 17 | + { |
| 18 | + "title": "apples", |
| 19 | + "count": [12000, 20000], |
| 20 | + "description": {"text": "...", "sensitive": false} |
| 21 | + }, |
| 22 | + { |
| 23 | + "title": "oranges", |
| 24 | + "count": [17500, null], |
| 25 | + "description": {"text": "...", "sensitive": false} |
| 26 | + } |
| 27 | + ] |
| 28 | + ``` |
| 29 | +``` |
| 30 | +<!-- markdownlint-disable MD046 --> |
| 31 | + |
| 32 | +Renders to: |
| 33 | + |
| 34 | +```json |
| 35 | +[ |
| 36 | + { |
| 37 | + "title": "apples", |
| 38 | + "count": [12000, 20000], |
| 39 | + "description": {"text": "...", "sensitive": false} |
| 40 | + }, |
| 41 | + { |
| 42 | + "title": "oranges", |
| 43 | + "count": [17500, null], |
| 44 | + "description": {"text": "...", "sensitive": false} |
| 45 | + } |
| 46 | +] |
| 47 | +``` |
| 48 | + |
| 49 | +## Supported languages |
| 50 | + |
| 51 | +Learn theme ships with its own version of highlight.js to support offline browsing. The included package supports 38 common languages, as described on the [highlight.js download page](https://highlightjs.org/download/). |
| 52 | + |
| 53 | +## Identifying failed language detection |
| 54 | + |
| 55 | +Highlight will write a warning to the browser console if a requested language was not found. For example, the following code block references an imaginary language `foo`. An error will be output to the console on this page. |
| 56 | + |
| 57 | +```plaintext |
| 58 | + ```foo |
| 59 | + bar |
| 60 | + ``` |
| 61 | +``` |
| 62 | + |
| 63 | +```nohighlight |
| 64 | +Could not find the language 'foo', did you forget to load/include a language module?(anonymous) @ highlight.pack.js |
| 65 | +``` |
| 66 | + |
| 67 | +## Supporting additional languages |
| 68 | + |
| 69 | +To support languages other than the 38 common languages included in the default highlight.js you will need to download your own version of highlight.js and add it to your site content. |
| 70 | + |
| 71 | +### Download custom highlight.js |
| 72 | + |
| 73 | +Visit [https://highlightjs.org/download/](https://highlightjs.org/download/) and select your desired language support. Note that more languages means greater package size. |
| 74 | + |
| 75 | +### Add custom highlight.js to static resources |
| 76 | + |
| 77 | +Inside the zip archive downloaded from highlight.js extract the file named `highlight.pack.js`. Move this file to the **new** location |
| 78 | + |
| 79 | +```nohighlight |
| 80 | +static/js/highlight.pack.js |
| 81 | +``` |
| 82 | + |
| 83 | +**Do not** replace the existing file at `themes/hugo-theme-learn/static/js/highlight.pack.js`. |
| 84 | + |
| 85 | +Including the file in the correct path will override the theme default highlight.pack.js and prevent issues caused in the future if the theme default file is updated. |
| 86 | + |
| 87 | +## Further usage information |
| 88 | + |
| 89 | +See [https://highlightjs.org/usage/](https://highlightjs.org/usage/) |
0 commit comments