Skip to content

Commit 404afbd

Browse files
authored
feat: highlightjs-diff — support code changes on posts (#303)
feat: `highlightjs-diff` — support code changes on posts
2 parents 1def5cd + dc08be4 commit 404afbd

File tree

5 files changed

+188
-6
lines changed

5 files changed

+188
-6
lines changed

.eleventy.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import fs from "fs"
22
import path from "path"
33
import dotenv from "dotenv"
44
import { DateTime } from "luxon"
5-
import hljs from "highlight.js"
5+
import highlight from "highlight.js"
6+
import highlightDiff from "highlightjs-code-diff"
67
import markdownIt from "markdown-it"
78
import anchor from "markdown-it-anchor"
89
import bracketedSpans from "markdown-it-bracketed-spans"
@@ -16,14 +17,17 @@ dotenv.config()
1617

1718
export default function (eleventyConfig) {
1819
const isDevelopment = process.env.NODE_ENV === "development"
20+
const hljs = highlightDiff(highlight)
1921
const markdownItConfig = markdownIt({
2022
html: true,
2123
linkify: true,
2224
typographer: true,
2325
highlight: function (str, lang) {
24-
if (lang && hljs.getLanguage(lang)) {
26+
const processLang = lang.replace("diff:", "")
27+
if (lang && hljs.getLanguage(processLang)) {
2528
try {
26-
return hljs.highlight(str, { language: lang }).value
29+
const returnValue = hljs.highlight(str, { language: lang }).value
30+
return returnValue
2731
} catch (__) { }
2832
}
2933
return ""

package-lock.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"dotenv": "^16.5.0",
4646
"glob": "^11.0.3",
4747
"highlight.js": "^11.5.1",
48+
"highlightjs-code-diff": "^1.0.0",
4849
"markdown-it-anchor": "^9.2.0",
4950
"markdown-it-attrs": "^4.1.4",
5051
"markdown-it-bracketed-spans": "^1.0.1",

src/articles/blog/2021-12-26-formatting-testing.md

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ tags:
2323
>
2424
> > `this is an inline-quote` does it works? click here to [jump to code-snippet-2](#code-snippet-2), or you can just scroll down, also you can hover on the code blocks.
2525
> >
26-
> > ```npm
26+
> > ```bash
2727
> > npm run install && npm run update
2828
> > ```
2929
> >
@@ -43,6 +43,7 @@ The markdown is being handle by the markdown-it and eleventy, while some extra w
4343
```sql
4444
SELECT * FROM `ikan`;
4545
```
46+
4647
```sql
4748
DROP table `ikan`;
4849
```
@@ -70,7 +71,7 @@ console.log(add(2, 3)) // Output: 5
7071

7172
Here is a simple JavaScript function that adds two numbers and logs the result.
7273

73-
```javascript
74+
```js
7475
const schedulerLoader = (schedules = {}) => {
7576
let time = new Date()
7677
let currentTime = time.getUTCHours() + 9
@@ -95,6 +96,37 @@ const schedulerLoader = (schedules = {}) => {
9596
}
9697
```
9798

99+
```diff:js
100+
const schedulerLoader = (schedules = {}) => {
101+
let time = new Date()
102+
let currentTime = time.getUTCHours() + 9
103+
if (currentTime > 23) currentTime -= 23
104+
105+
if (replaceStatus) replaceStatus.textContent = 'Idle'
106+
- setTimeout(() => throw new Error(),0)
107+
+ setTimeout(() => {
108+
+ for (let i = 0; i < schedules.length; i++) {
109+
+ let { day, endHour, startHour } = schedules[i]
110+
+ if (!replaceStatus) return
111+
+
112+
+ if (days[currentDay] === toCapitalizeWord(day)) {
113+
+ if (currentTime > startHour && currentTime < endHour) {
114+
+ replaceStatus.textContent = 'Studying'
115+
+ }
116+
+ break
117+
+ }
118+
+ replaceStatus.textContent = 'Idle'
119+
+ }
120+
+ }, 0)
121+
```
122+
123+
```md
124+
├─ docs
125+
│ ├─ .vuepress
126+
│ │ └─ config.js
127+
│ └─ README.md
128+
└─ package.json
129+
```
98130
And this is an example of markdown list:
99131

100132
```md
@@ -115,7 +147,6 @@ And this is an example of markdown list:
115147
2. what
116148
3. don't worry
117149

118-
119150
#### Code snippet 3
120151

121152
```js

src/assets/scss/highlight.scss

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
pre code.hljs {
2+
display: block;
3+
overflow-x: auto;
4+
padding: 1em
5+
}
6+
7+
code.hljs {
8+
padding: 3px 5px
9+
}
10+
11+
/*!
12+
Theme: Tokyo-night-Dark
13+
origin: https://github.com/enkia/tokyo-night-vscode-theme
14+
Description: Original highlight.js style
15+
Author: (c) Henri Vandersleyen <hvandersleyen@gmail.com>
16+
License: see project LICENSE
17+
Touched: 2022
18+
*/
19+
.hljs-comment,
20+
.hljs-meta {
21+
color: #565f89
22+
}
23+
24+
.hljs-deletion,
25+
.hljs-doctag,
26+
.hljs-regexp,
27+
.hljs-selector-attr,
28+
.hljs-selector-class,
29+
.hljs-selector-id,
30+
.hljs-selector-pseudo,
31+
.hljs-tag,
32+
.hljs-template-tag,
33+
.hljs-variable.language_ {
34+
color: #f7768e
35+
}
36+
37+
.hljs-link,
38+
.hljs-literal,
39+
.hljs-number,
40+
.hljs-params,
41+
.hljs-template-variable,
42+
.hljs-type,
43+
.hljs-variable {
44+
color: #ff9e64
45+
}
46+
47+
.hljs-attribute,
48+
.hljs-built_in {
49+
color: #e0af68
50+
}
51+
52+
.hljs-keyword,
53+
.hljs-property,
54+
.hljs-subst,
55+
.hljs-title,
56+
.hljs-title.class_,
57+
.hljs-title.class_.inherited__,
58+
.hljs-title.function_ {
59+
color: #7dcfff
60+
}
61+
62+
.hljs-selector-tag {
63+
color: #73daca
64+
}
65+
66+
.hljs-addition,
67+
.hljs-bullet,
68+
.hljs-quote,
69+
.hljs-string,
70+
.hljs-symbol {
71+
color: #9ece6a
72+
}
73+
74+
.hljs-code,
75+
.hljs-formula,
76+
.hljs-section {
77+
color: #7aa2f7
78+
}
79+
80+
.hljs-attr,
81+
.hljs-char.escape_,
82+
.hljs-keyword,
83+
.hljs-name,
84+
.hljs-operator {
85+
color: #bb9af7
86+
}
87+
88+
.hljs-punctuation {
89+
color: #c0caf5
90+
}
91+
92+
.hljs {
93+
background: #1a1b26;
94+
color: #9aa5ce
95+
}
96+
97+
.hljs-emphasis {
98+
font-style: italic
99+
}
100+
101+
.hljs-strong {
102+
font-weight: 700
103+
}
104+
105+
/*!
106+
Author: (c) EmptyWork <contact@emptywork.my.id>
107+
Touched: 2025
108+
109+
Additional styling for diff support
110+
*/
111+
112+
.hljs-addition,
113+
.hljs-deletion {
114+
--_distance: 1em;
115+
--_border: 0em;
116+
--_offset: 0.61875em;
117+
display: inline-block;
118+
width: calc(100% + calc(var(--_distance) * 2));
119+
margin-inline: calc(var(--_distance) * -1);
120+
padding-inline: 1em;
121+
padding-left: calc(0.5em - var(--_border) + var(--_offset));
122+
}
123+
124+
.hljs-addition,
125+
.hljs-deletion {
126+
--_border: 0.25em;
127+
--_offset: 0em;
128+
--_color: 349, 89%, 72%;
129+
background-color: hsl(var(--_color), 0.2);
130+
border-left: var(--_border) solid hsl(var(--_color), 0.2);
131+
}
132+
133+
.hljs-addition {
134+
--_color: 89, 51%, 61%;
135+
}
136+
137+
.hljs-deletion {
138+
--_color: 349, 89%, 72%;
139+
}

0 commit comments

Comments
 (0)