Skip to content

Commit 708f230

Browse files
authored
Highlight js dark example (#171)
* use a11y theme * example dark theme site * update links * add option to deploy dark site
1 parent 0934f64 commit 708f230

File tree

10 files changed

+173
-5
lines changed

10 files changed

+173
-5
lines changed

.github/workflows/pages-deploy-example-site.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ on:
1717
type: choice
1818
options:
1919
- highlightjs
20+
- highlightjs-dark
2021

2122
jobs:
2223
deploy-highlightjs-example:
@@ -29,3 +30,14 @@ jobs:
2930
example-site-name: 'highlightjs'
3031
secrets:
3132
GH_DEPLOY_KEY: ${{ secrets.DEPLOY_KEY_HIGHLIGHTJS }}
33+
34+
deploy-highlightjs-dark-example:
35+
name: Deploy ${{ inputs.example-site-name }}
36+
if: inputs.example-site-name == 'highlightjs-dark'
37+
uses: ntno/mkdocs-terminal/.github/workflows/reusable-pages-deploy.yml@main
38+
with:
39+
tag: ${{ inputs.tag }}
40+
mkdocs-terminal-version: ${{ inputs.mkdocs-terminal-version }}
41+
example-site-name: 'highlightjs-dark'
42+
secrets:
43+
GH_DEPLOY_KEY: ${{ secrets.DEPLOY_KEY_HIGHLIGHTJS_DARK }}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
window.addEventListener('load', hljs_highlight, false);
2+
3+
function hljs_highlight() {
4+
hljs.highlightAll();
5+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# mkdocs-terminal with highlight.js
2+
3+
This example site uses the [highlight.js] javascript library to add code highlighting to a page after it is loaded in the user's web browser.
4+
5+
Please review the [highlight.js docs] for more information:
6+
7+
- [supported languages]
8+
- [style options]
9+
- [get the latest files]
10+
11+
[highlight.js]: https://highlightjs.org/
12+
[highlight.js docs]: https://highlightjs.readthedocs.io/en/latest/readme.html
13+
[supported languages]: https://highlightjs.readthedocs.io/en/latest/supported-languages.html
14+
[get the latest files]: https://cdnjs.com/libraries/highlight.js
15+
[style options]: https://highlightjs.org/examples
16+
17+
## Set Up
18+
### Add highlight.js
19+
20+
Specify the `highlight.js` javascript and CSS source files. At a minimum you will need to include the main script `highlight.min.js` and stylesheet `a11y-dark.min.css`. You should also specify language specific scripts as needed.
21+
22+
`mkdocs.yml` excerpt:
23+
24+
```yaml
25+
theme:
26+
name: terminal
27+
palette: dark
28+
29+
extra_javascript:
30+
- https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.0/highlight.min.js
31+
- https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.0/languages/bash.min.js
32+
- https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.0/languages/javascript.min.js
33+
- https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.0/languages/python.min.js
34+
- highlight.js
35+
36+
extra_css:
37+
- https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.0/styles/a11y-dark.min.css
38+
```
39+
40+
### Add Custom Highlighter Code
41+
42+
Add a custom javascript file which will trigger the `highlight.js` library to highlight code blocks after an HTML page loads. You can add the file to `docs/highlight.js` and reference it in the `extra_javascript` section of `mkdocs.yml`:
43+
44+
```javascript
45+
window.addEventListener('load', hljs_highlight, false);
46+
47+
function hljs_highlight() {
48+
hljs.highlightAll();
49+
}
50+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Bash
2+
3+
Bash language highlighting with `highlight.js`:
4+
5+
```bash
6+
#!/bin/bash
7+
echo "Today is " `date`
8+
9+
echo -e "\nenter the path to a directory"
10+
read the_path
11+
12+
echo -e "\nfiles and folders: "
13+
ls $the_path
14+
```
15+
16+
Script adapted from Zaira Hira's ["Bash Scripting Tutorial Linux Shell Script and Command Line For Beginners"](https://www.freecodecamp.org/news/bash-scripting-tutorial-linux-shell-script-and-command-line-for-beginners/)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Javascript
2+
3+
Javascript language highlighting with `highlight.js`:
4+
5+
```javascript
6+
let score1 = 2;
7+
let score2 = 5;
8+
let averageScore = (score1 + score2) / 2.0
9+
console.log(averageScore)
10+
```
11+
12+
Script adapted from Austin Asoluka's ["How to Use Variables and Data Types in JavaScript – Explained With Code Examples"](https://www.freecodecamp.org/news/how-to-use-variables-and-data-types-in-javascript/)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Python
2+
3+
Python language highlighting with `highlight.js`:
4+
5+
```python
6+
# calculate factorial of n
7+
def fact(n):
8+
9+
# no work required
10+
if n == 1 or n == 0:
11+
return 1
12+
13+
# minimum amount of work
14+
return n * fact(n - 1)
15+
16+
n = 5
17+
18+
# calculate factorial
19+
factorial = fact(n)
20+
print(f"{n}! = {factorial}")
21+
```
22+
23+
Script adapted from Palistha Singh's ["How Does Recursion Work? Explained with Code Examples"](https://www.freecodecamp.org/news/what-is-recursion/)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
site_name: mkdocs-terminal dark theme with highlight.js
2+
site_url: https://ntno.github.io/mkdocs-terminal-example-highlightjs-dark
3+
repo_url: https://github.com/ntno/mkdocs-terminal
4+
edit_uri_template: https://github.com/ntno/mkdocs-terminal/edit/main/tests/examples/highlightjs-dark/docs/{path}
5+
copyright: Copyright 2024 Natan Organick, All rights reserved
6+
site_author: ntno
7+
8+
nav:
9+
- Configuration: 'index.md'
10+
- Languages:
11+
- Bash: 'sample-languages/bash.md'
12+
- Python: 'sample-languages/python.md'
13+
- Javascript: 'sample-languages/javascript.md'
14+
15+
plugins:
16+
- git-revision-date
17+
18+
markdown_extensions:
19+
- toc:
20+
baselevel: "1"
21+
toc_depth: "2-4"
22+
permalink: "#"
23+
permalink_title: Anchor link to this section for reference
24+
25+
theme:
26+
name: terminal
27+
palette: dark
28+
features:
29+
- footer.prev_next
30+
- navigation.top.search_button.hide
31+
- navigation.side.indexes
32+
- revision.date
33+
- revision.history
34+
35+
extra_javascript:
36+
- https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.0/highlight.min.js
37+
- https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.0/languages/bash.min.js
38+
- https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.0/languages/javascript.min.js
39+
- https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.0/languages/python.min.js
40+
- highlight.js
41+
42+
extra_css:
43+
- https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.0/styles/a11y-dark.min.css
44+
45+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mkdocs
2+
mkdocs-terminal~=4.0
3+
mkdocs-git-revision-date-plugin

tests/examples/highlightjs/docs/index.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ This example site uses the [highlight.js] javascript library to add code highlig
55
Please review the [highlight.js docs] for more information:
66

77
- [supported languages]
8-
- [get the latest files from a CDN]
8+
- [style options]
9+
- [get the latest files]
910

1011
[highlight.js]: https://highlightjs.org/
1112
[highlight.js docs]: https://highlightjs.readthedocs.io/en/latest/readme.html
1213
[supported languages]: https://highlightjs.readthedocs.io/en/latest/supported-languages.html
13-
[get the latest files from a CDN]: https://highlightjs.readthedocs.io/en/latest/readme.html#fetch-via-cdn
14+
[get the latest files]: https://cdnjs.com/libraries/highlight.js
15+
[style options]: https://highlightjs.org/examples
1416

1517
## Set Up
1618
### Add highlight.js
1719

18-
Specify the `highlight.js` javascript and CSS source files. At a minimum you will need to include the main script `highlight.min.js` and stylesheet `default.min.css`. You should also specify language specific scripts as needed.
20+
Specify the `highlight.js` javascript and CSS source files. At a minimum you will need to include the main script `highlight.min.js` and stylesheet `a11y-light.min.css`. You should also specify language specific scripts as needed.
1921

2022
`mkdocs.yml` excerpt:
2123

@@ -31,7 +33,7 @@ extra_javascript:
3133
- highlight.js
3234

3335
extra_css:
34-
- https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.0/styles/default.min.css
36+
- https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.0/styles/a11y-light.min.css
3537
```
3638
3739
### Add Custom Highlighter Code

tests/examples/highlightjs/mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ extra_javascript:
3939
- highlight.js
4040

4141
extra_css:
42-
- https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.0/styles/default.min.css
42+
- https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.0/styles/a11y-light.min.css
4343

4444

0 commit comments

Comments
 (0)