Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit 61ebbd9

Browse files
feat(highlight-code): add line numbers
1 parent 3fe0c22 commit 61ebbd9

File tree

5 files changed

+74
-12
lines changed

5 files changed

+74
-12
lines changed

docs/src/app/pages/app-home/app-home.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ app-home {
8888
deckgo-highlight-code {
8989
--deckgo-highlight-code-background: var(--ion-color-dark);
9090
--deckgo-highlight-code-color: white;
91+
--deckgo-highlight-code-line-number: #999;
92+
--deckgo-highlight-code-line-divider: #999;
9193
}
9294
}
9395

docs/src/global/theme/code.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ deckgo-highlight-code {
88
--deckgo-highlight-code-border-radius: 8px;
99
--deckgo-highlight-code-background: var(--ion-color-light);
1010
--deckgo-highlight-code-color: inherit;
11+
12+
--deckgo-hightlight-code-line-number: #999;
13+
--deckgo-hightlight-code-line-divider: #999;
1114
}
1215

1316
deckgo-highlight-code, deckgo-deck {

webcomponents/highlight-code/src/components.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
DeckdeckgoHighlightCodeAnchor,
1111
} from './components/declarations/deckdeckgo-highlight-code-anchor';
1212

13-
1413
export namespace Components {
1514
interface DeckgoHighlightCode {
1615
'anchor': string;
@@ -20,6 +19,7 @@ export namespace Components {
2019
'hideAnchor': boolean;
2120
'highlightLines': string;
2221
'language': string;
22+
'lineNumbers': boolean;
2323
'load': () => Promise<void>;
2424
'src': string;
2525
'zoomCode': (zoom: boolean) => Promise<void>;
@@ -47,6 +47,7 @@ declare namespace LocalJSX {
4747
'hideAnchor'?: boolean;
4848
'highlightLines'?: string;
4949
'language'?: string;
50+
'lineNumbers'?: boolean;
5051
'onCodeDidChange'?: (event: CustomEvent<HTMLElement>) => void;
5152
'onPrismLanguageLoaded'?: (event: CustomEvent<string>) => void;
5253
'src'?: string;

webcomponents/highlight-code/src/components/highlight-code/deckdeckgo-highlight-code.scss

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,25 @@ div.deckgo-highlight-code-container {
4848

4949
display: var(--deckgo-highlight-code-display, block);
5050

51+
counter-reset: linenumber;
52+
53+
> div.deckgo-highlight-code-line-number {
54+
counter-increment: linenumber;
55+
position: relative;
56+
padding-left: 3.5em;
57+
58+
&:before {
59+
content: counter(linenumber);
60+
min-height: 1rem;
61+
display: inline-block;
62+
position: absolute;
63+
left: 0;
64+
width: 2.5em;
65+
border-right: 1px solid var(--deckgo-highlight-code-line-number, #999);
66+
color: var(--deckgo-highlight-code-line-divider, #999);
67+
}
68+
}
69+
5170
div:empty {
5271
min-height: 1rem;
5372
}
@@ -128,3 +147,25 @@ div.deckgo-highlight-code-container {
128147
}
129148
}
130149
}
150+
151+
// code {
152+
// counter-reset: linenumber;
153+
// white-space: pre-wrap;
154+
155+
// & > div {
156+
// counter-increment: linenumber;
157+
// position: relative;
158+
// padding-left: 3.5em;
159+
160+
// &:before {
161+
// content: counter(linenumber);
162+
// min-height: 1rem;
163+
// display: inline-block;
164+
// position: absolute;
165+
// left: 0;
166+
// width: 2.5em;
167+
// border-right: 1px solid #999;
168+
// color: #999;
169+
// }
170+
// }
171+
// }

webcomponents/highlight-code/src/components/highlight-code/deckdeckgo-highlight-code.tsx

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import {Component, Prop, Watch, Element, Method, EventEmitter, Event, Listen, State, h, Host} from '@stencil/core';
1+
import { Component, Prop, Watch, Element, Method, EventEmitter, Event, Listen, State, h, Host } from '@stencil/core';
22

33
import Prism from 'prismjs';
44

5-
import {DeckdeckgoHighlightCodeAnchor} from '../declarations/deckdeckgo-highlight-code-anchor';
5+
import { DeckdeckgoHighlightCodeAnchor } from '../declarations/deckdeckgo-highlight-code-anchor';
66

77
@Component({
88
tag: 'deckgo-highlight-code',
@@ -22,9 +22,10 @@ export class DeckdeckgoHighlightCode {
2222
@Prop() anchorZoom: string = '// DeckDeckGoZoom';
2323
@Prop() hideAnchor: boolean = true;
2424

25-
@Prop({reflectToAttr: true}) language: string = 'javascript';
25+
@Prop({ reflectToAttr: true }) language: string = 'javascript';
2626

27-
@Prop({reflectToAttr: true}) highlightLines: string;
27+
@Prop({ reflectToAttr: true }) highlightLines: string;
28+
@Prop() lineNumbers: boolean = true;
2829

2930
@Prop() editable: boolean = false;
3031

@@ -42,7 +43,7 @@ export class DeckdeckgoHighlightCode {
4243
}
4344
}
4445

45-
@Listen('prismLanguageLoaded', {target: 'document'})
46+
@Listen('prismLanguageLoaded', { target: 'document' })
4647
async languageLoaded($event: CustomEvent) {
4748
if (!$event || !$event.detail) {
4849
return;
@@ -178,9 +179,23 @@ export class DeckdeckgoHighlightCode {
178179

179180
if (container) {
180181
try {
181-
const highlightedCode: string = Prism.highlight(code, Prism.languages[this.language], this.language);
182+
if (this.lineNumbers) {
183+
// split the code on linebreaks
184+
const regEx = RegExp(/\n(?!$)/g); //
185+
const match = code.split(regEx);
186+
match.forEach(m => {
187+
let div: HTMLElement = document.createElement('div');
188+
div.classList.add('deckgo-highlight-code-line-number');
189+
190+
let highlight: string = Prism.highlight(m, Prism.languages[this.language], this.language);
191+
div.innerHTML = highlight;
192+
container.children[0].appendChild(div);
193+
});
194+
}else{
195+
container.children[0].innerHTML = Prism.highlight(code, Prism.languages[this.language], this.language);
196+
}
197+
182198

183-
container.children[0].innerHTML = highlightedCode;
184199

185200
await this.addAnchors();
186201

@@ -388,7 +403,7 @@ export class DeckdeckgoHighlightCode {
388403
if (slottedCode) {
389404
setTimeout(() => {
390405
slottedCode.setAttribute('contentEditable', 'true');
391-
slottedCode.addEventListener('blur', this.applyCode, {once: true});
406+
slottedCode.addEventListener('blur', this.applyCode, { once: true });
392407
slottedCode.addEventListener('keydown', this.catchNewLine);
393408

394409
slottedCode.focus();
@@ -458,10 +473,10 @@ export class DeckdeckgoHighlightCode {
458473

459474
render() {
460475
return (
461-
<Host class={{'deckgo-highlight-code-edit': this.editing}}>
476+
<Host class={{ 'deckgo-highlight-code-edit': this.editing }}>
462477
<div class="deckgo-highlight-code-container"
463-
onMouseDown={() => this.edit()}
464-
onTouchStart={() => this.edit()}>
478+
onMouseDown={() => this.edit()}
479+
onTouchStart={() => this.edit()}>
465480
<code></code>
466481
<slot name="code"></slot>
467482
</div>

0 commit comments

Comments
 (0)