1
+ import highlight from "highlight.js"
2
+ import highlightjsLines from "./highlightjs-lines.js"
3
+ import highlightDiff from "highlightjs-code-diff"
4
+ import markdownIt from "markdown-it"
5
+ import { describe , it } from 'node:test'
6
+ import assert from "node:assert"
7
+
8
+ const hljsLinesWithDiff = highlightjsLines ( highlightDiff ( highlight ) )
9
+ const hljsLines = highlightjsLines ( highlight )
10
+
11
+ describe ( 'Highlight Lines using Markdown With Diff' , ( ) => {
12
+ const mIt = markdownIt ( {
13
+ html : true ,
14
+ linkify : true ,
15
+ typographer : true ,
16
+ highlight : function ( str , lang ) {
17
+ if ( lang ) {
18
+ try {
19
+ return hljsLinesWithDiff . highlight ( str , { language : lang } ) . value
20
+ } catch ( __ ) { }
21
+ }
22
+ return ""
23
+ }
24
+ } )
25
+ it ( 'Test With Diff (Deletion)' , ( ) => {
26
+ const data = `
27
+ \`\`\`diff:js
28
+ - setTimeout(() => throw new Error(),0)
29
+ \`\`\`
30
+ `
31
+ const value = mIt . render ( data )
32
+ assert . strictEqual ( value , `<pre><code class="language-diff:js"><span class="hljs-deletion"><span class='hljs-indicator'>-</span><span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(),<span class="hljs-number">0</span>)</span>\n</code></pre>\n` )
33
+ } )
34
+ it ( 'Test With Diff (Addition)' , ( ) => {
35
+ const data = `
36
+ \`\`\`diff:js
37
+ + setTimeout(() => throw new Error(),0)
38
+ \`\`\`
39
+ `
40
+ const value = mIt . render ( data )
41
+ assert . strictEqual ( value , `<pre><code class="language-diff:js"><span class="hljs-addition"><span class='hljs-indicator'>+</span><span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(),<span class="hljs-number">0</span>)</span>\n</code></pre>\n` )
42
+ } )
43
+ it ( 'Normal Test' , ( ) => {
44
+ const data = `
45
+ \`\`\`js
46
+ let time = new Date()
47
+ \`\`\`
48
+ `
49
+ const value = mIt . render ( data )
50
+ assert . strictEqual ( value , `<pre><code class="language-js"><span class="hljs-line"><span class="hljs-keyword">let</span> time = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>()</span>\n</code></pre>\n` )
51
+ } )
52
+ } )
53
+
54
+ describe ( "Highlight Lines with Diff" , async ( t ) => {
55
+ it ( "Test With Diff (Deletion)" , ( ) => {
56
+ const code = ` function helloWorld () {
57
+ - return 'Hello'
58
+ }
59
+ `
60
+ const { value } = hljsLinesWithDiff . highlight ( code , { language : 'diff:js' } )
61
+ assert . strictEqual ( value , `<span class="hljs-line"> <span class="hljs-keyword">function</span> <span class="hljs-title function_">helloWorld</span> (<span class="hljs-params"></span>) {</span>
62
+ <span class="hljs-deletion"><span class='hljs-indicator'>-</span> <span class="hljs-keyword">return</span> <span class="hljs-string">'Hello'</span></span>
63
+ <span class="hljs-line"> }</span>\n` )
64
+ } )
65
+ it ( "Test With Diff (Addition)" , ( ) => {
66
+ const code = ` function helloWorld () {
67
+ + return 'Hello, world!'
68
+ }
69
+ `
70
+ const { value } = hljsLinesWithDiff . highlight ( code , { language : 'diff:js' } )
71
+ assert . strictEqual ( value , `<span class="hljs-line"> <span class="hljs-keyword">function</span> <span class="hljs-title function_">helloWorld</span> (<span class="hljs-params"></span>) {</span>
72
+ <span class="hljs-addition"><span class='hljs-indicator'>+</span> <span class="hljs-keyword">return</span> <span class="hljs-string">'Hello, world!'</span></span>
73
+ <span class="hljs-line"> }</span>\n` )
74
+ } )
75
+ it ( "Normal Test" , ( ) => {
76
+ const code = ` function helloWorld () {
77
+ return 'Hello'
78
+ return 'Hello, world!'
79
+ }
80
+ `
81
+ const { value } = hljsLinesWithDiff . highlight ( code , { language : 'diff:js' } )
82
+ assert . strictEqual ( value , `<span class="hljs-line"> <span class="hljs-keyword">function</span> <span class="hljs-title function_">helloWorld</span> (<span class="hljs-params"></span>) {</span>
83
+ <span class="hljs-line"> <span class="hljs-keyword">return</span> <span class="hljs-string">'Hello'</span></span>
84
+ <span class="hljs-line"> <span class="hljs-keyword">return</span> <span class="hljs-string">'Hello, world!'</span></span>
85
+ <span class="hljs-line"> }</span>\n` )
86
+ } )
87
+ } )
88
+
89
+ describe ( "Highlight Lines" , async ( t ) => {
90
+ it ( "Test With Diff (Deletion)" , ( ) => {
91
+ const code = ` function helloWorld () {
92
+ - return 'Hello'
93
+ }
94
+ `
95
+ const { value } = hljsLines . highlight ( code , { language : 'js' } )
96
+ assert . strictEqual ( value , `<span class="hljs-line"> <span class="hljs-keyword">function</span> <span class="hljs-title function_">helloWorld</span> (<span class="hljs-params"></span>) {</span>
97
+ <span class="hljs-line">- <span class="hljs-keyword">return</span> <span class="hljs-string">'Hello'</span></span>
98
+ <span class="hljs-line"> }</span>\n` )
99
+ } )
100
+ it ( "Test With Diff (Addition)" , ( ) => {
101
+ const code = ` function helloWorld () {
102
+ + return 'Hello, world!'
103
+ }
104
+ `
105
+ const { value } = hljsLines . highlight ( code , { language : 'js' } )
106
+ assert . strictEqual ( value , `<span class="hljs-line"> <span class="hljs-keyword">function</span> <span class="hljs-title function_">helloWorld</span> (<span class="hljs-params"></span>) {</span>
107
+ <span class="hljs-line">+ <span class="hljs-keyword">return</span> <span class="hljs-string">'Hello, world!'</span></span>
108
+ <span class="hljs-line"> }</span>\n` )
109
+ } )
110
+ it ( "Normal Test" , ( ) => {
111
+ const code = ` function helloWorld () {
112
+ return 'Hello'
113
+ return 'Hello, world!'
114
+ }
115
+ `
116
+ const { value } = hljsLines . highlight ( code , { language : 'js' } )
117
+ assert . strictEqual ( value , `<span class="hljs-line"> <span class="hljs-keyword">function</span> <span class="hljs-title function_">helloWorld</span> (<span class="hljs-params"></span>) {</span>
118
+ <span class="hljs-line"> <span class="hljs-keyword">return</span> <span class="hljs-string">'Hello'</span></span>
119
+ <span class="hljs-line"> <span class="hljs-keyword">return</span> <span class="hljs-string">'Hello, world!'</span></span>
120
+ <span class="hljs-line"> }</span>\n` )
121
+ } )
122
+ } )
0 commit comments