Skip to content

Commit 31abcac

Browse files
authored
feat(task): implement codeblock line numbering (#103)
1 parent 87c1154 commit 31abcac

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/app/components/tasks/task-body/task-body.component.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,31 @@ import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
1616
import { TaskTipComponent } from './task-tip/task-tip.component';
1717
import { TaskPanel, TaskTipData } from '../../../models';
1818

19+
// https://stackoverflow.com/questions/64280814/how-can-i-correctly-highlight-a-line-by-line-code-using-highlight-js-react
20+
highlight.addPlugin({
21+
'after:highlight': (params: { value: string }) => {
22+
const openTags: string[] = [];
23+
24+
params.value = params.value
25+
.split('\n')
26+
.map((line) => {
27+
line = line.replace(/(<span [^>]+>)|(<\/span>)/g, (match) => {
28+
if (match === '</span>') {
29+
openTags.pop();
30+
} else {
31+
openTags.push(match);
32+
}
33+
return match;
34+
});
35+
36+
return `<div>${openTags.join('')}${line}${'</span>'.repeat(
37+
openTags.length
38+
)}</div>`;
39+
})
40+
.join('');
41+
},
42+
});
43+
1944
@Component({
2045
selector: 'ksi-task-body',
2146
templateUrl: './task-body.component.html',

src/styles.scss

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,25 @@ Codemirror core style
5959
}
6060
}
6161

62+
pre.numberLines {
63+
counter-reset: line;
64+
}
65+
66+
pre.numberLines code {
67+
display: block;
68+
}
69+
70+
pre.numberLines code div::before {
71+
content: counter(line);
72+
counter-increment: line;
73+
display: inline-block;
74+
text-align: right;
75+
color: #999;
76+
border-right: 1px solid #ddd;
77+
padding: 0 0.5em 0 0.1em;
78+
margin-right: 6px;
79+
}
80+
6281
table {
6382
th, td {
6483
color: $ksi-page-fg;

0 commit comments

Comments
 (0)