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

Commit 25d6804

Browse files
Merge pull request #213 from StefanNieuwenhuis/master
Add line numbers to highlight code
2 parents 50bfe35 + d0c3a5c commit 25d6804

File tree

8 files changed

+78
-17
lines changed

8 files changed

+78
-17
lines changed

docs/docs/components/app-components-highlight-code/app-components-highlight-code.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ The `<deckgo-highlight-code/>` could be styled using the following CSS4 variable
9191
| --deckgo-highlight-code-font-size | | The size of the font for the code |
9292
| --deckgo-highlight-code-font-family | monospace | The family of the font for the code |
9393
| --deckgo-highlight-code-line-background | #3880ff | The background of the lines you wish to highlight |
94+
| --deckgo-highlight-code-line-divider | #999999 | The color of the line numbers |
95+
| --deckgo-highlight-code-line-number | #999999 | The color of the divider between line numbers and code |
9496
| --deckgo-highlight-code-line-padding | | A padding for each lines you wish to highlight |
9597
| --deckgo-highlight-code-line-border-top | | The border-top property of the lines you wish to highlight |
9698
| --deckgo-highlight-code-direction | ltr | The direction of the displayed code |

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: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ div.deckgo-highlight-code-container {
3939
text-align: var(--deckgo-highlight-code-text-align, start);
4040

4141
code {
42-
overflow-y: var(--deckgo-highlight-code-scroll, scroll);;
42+
overflow-y: var(--deckgo-highlight-code-scroll, scroll);
4343

4444
white-space: pre-wrap;
4545

@@ -48,6 +48,26 @@ 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+
background: var(--deckgo-highlight-code-background, white);
66+
border-right: 1px solid var(--deckgo-highlight-code-line-number, #999);
67+
color: var(--deckgo-highlight-code-line-divider, #999);
68+
}
69+
}
70+
5171
div:empty {
5272
min-height: 1rem;
5373
}
@@ -56,9 +76,8 @@ div.deckgo-highlight-code-container {
5676
visibility: hidden;
5777
}
5878

59-
span.deckgo-highlight-code-line {
79+
.deckgo-highlight-code-line {
6080
background: var(--deckgo-highlight-code-line-background, #3880ff);
61-
padding: var(--deckgo-highlight-code-line-padding);
6281
border-top: var(--deckgo-highlight-code-line-border-top);
6382
border-bottom: var(--deckgo-highlight-code-line-border-bottom);
6483
}

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

Lines changed: 29 additions & 13 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;
@@ -173,14 +174,30 @@ export class DeckdeckgoHighlightCode {
173174

174175
private parseCode(code: string): Promise<void> {
175176
return new Promise<void>(async (resolve, reject) => {
176-
177177
const container: HTMLElement = this.el.shadowRoot.querySelector('div.deckgo-highlight-code-container');
178178

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

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

185202
await this.addAnchors();
186203

@@ -233,7 +250,6 @@ export class DeckdeckgoHighlightCode {
233250
if (this.highlightLines && this.highlightLines.length > 0) {
234251

235252
const rows: number[] = await this.findRowsToHighlight();
236-
237253
if (rows && rows.length > 0) {
238254
const containerCode: HTMLElement = this.el.shadowRoot.querySelector('code');
239255

@@ -388,7 +404,7 @@ export class DeckdeckgoHighlightCode {
388404
if (slottedCode) {
389405
setTimeout(() => {
390406
slottedCode.setAttribute('contentEditable', 'true');
391-
slottedCode.addEventListener('blur', this.applyCode, {once: true});
407+
slottedCode.addEventListener('blur', this.applyCode, { once: true });
392408
slottedCode.addEventListener('keydown', this.catchNewLine);
393409

394410
slottedCode.focus();
@@ -458,10 +474,10 @@ export class DeckdeckgoHighlightCode {
458474

459475
render() {
460476
return (
461-
<Host class={{'deckgo-highlight-code-edit': this.editing}}>
477+
<Host class={{ 'deckgo-highlight-code-edit': this.editing }}>
462478
<div class="deckgo-highlight-code-container"
463-
onMouseDown={() => this.edit()}
464-
onTouchStart={() => this.edit()}>
479+
onMouseDown={() => this.edit()}
480+
onTouchStart={() => this.edit()}>
465481
<code></code>
466482
<slot name="code"></slot>
467483
</div>

webcomponents/highlight-code/src/components/highlight-code/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
| `hideAnchor` | `hide-anchor` | | `boolean` | `true` |
1616
| `highlightLines` | `highlight-lines` | | `string` | `undefined` |
1717
| `language` | `language` | | `string` | `'javascript'` |
18+
| `lineNumbers` | `line-numbers` | | `boolean` | `true` |
1819
| `src` | `src` | | `string` | `undefined` |
1920

2021

webcomponents/highlight-code/src/index.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,22 @@ <h1>Plain code and java language:</h1>
8787
Markdown</code>
8888
</deckgo-highlight-code>
8989

90+
<h1>Without line numbers</h1>
91+
<deckgo-highlight-code language="java" line-numbers="false" highlight-lines="0,2 6,6">
92+
<code slot="code">interface NumericTest {
93+
boolean computeTest(int n);
94+
}
95+
96+
public static void main(String args[]) {
97+
NumericTest isEven = (n) -> (n % 2) == 0;
98+
NumericTest isNegative = (n) -> (n < 0);
99+
100+
// Output: false
101+
System.out.println(isEven.computeTest(5));
102+
103+
// Output: true
104+
System.out.println(isNegative.computeTest(-5));
105+
}</code>
106+
</deckgo-highlight-code>
90107
</body>
91108
</html>

0 commit comments

Comments
 (0)