Skip to content

Commit 4eb76d8

Browse files
authored
Merge pull request #57 from fry69/diff-info-fix
Fix data-diff first line bug
2 parents 142070d + ea187dd commit 4eb76d8

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

addon/initializers/showdown-extension.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ function stripQuotes(string) {
2525
return string;
2626
}
2727

28+
function diffInfo(args, codeblock) {
29+
if (args) {
30+
let lines = codeblock.split('\n');
31+
32+
args.forEach(pD => {
33+
let operator = pD[0];
34+
let lineNo = +(pD.replace(operator, ''));
35+
let text = lines[lineNo - 1];
36+
if (operator === '+') {
37+
lines[lineNo - 1] = `<span class="diff-insertion"><span class="diff-operator">+</span>${text}</span>`;
38+
} else {
39+
lines[lineNo - 1] = `<span class="diff-deletion"><span class="diff-operator">-</span>${text}</span>`;
40+
}
41+
});
42+
codeblock = lines.join('\n');
43+
}
44+
return codeblock;
45+
}
46+
2847
export function initialize(/* application */) {
2948
showdown.subParser('githubCodeBlocks', function (text, options, globals) {
3049
// early exit if option is not enabled
@@ -70,6 +89,8 @@ export function initialize(/* application */) {
7089
let lineNumbersHTML = getLineNumbersHTML(idCounter, codeblock);
7190
idCounter++;
7291

92+
let diffInfoArgs = attributes['data-diff']?.split(',');
93+
7394
assert(`Language "${language}" not found. Have you configured Prism correctly?`, !language || Prism.languages[language]);
7495

7596
if (language && Prism.languages[language]) {
@@ -79,6 +100,7 @@ export function initialize(/* application */) {
79100
codeblock = codeblock.replace(/¨T/g, '¨');
80101

81102
let highlightedCodeBlock = Prism.highlight(codeblock, Prism.languages[language], language) + end;
103+
highlightedCodeBlock = diffInfo(diffInfoArgs, highlightedCodeBlock);
82104
codeblock = `<pre class="language-${language} line-numbers"><code ${language ? `class="${language} language-${language}"` : ''}>${highlightedCodeBlock}${lineNumbersHTML}</code></pre>`;
83105

84106
// Convert to the special characters Showdown uses again
@@ -89,27 +111,10 @@ export function initialize(/* application */) {
89111
codeblock = `<div class="filename ${language}"><div class="ribbon"></div><span>${attributes['data-filename'] || ''}</span>${codeblock}</div>`;
90112
}
91113
} else {
114+
codeblock = diffInfo(diffInfoArgs, codeblock);
92115
codeblock = `<pre class="language-none line-numbers"><code class="language-none">${codeblock}${lineNumbersHTML}</code></pre>`;
93116
}
94117

95-
const diffInfo = attributes['data-diff']?.split(',');
96-
97-
if (diffInfo) {
98-
let lines = codeblock.split('\n');
99-
100-
diffInfo.forEach(pD => {
101-
let operator = pD[0];
102-
let lineNo = +(pD.replace(operator, ''));
103-
let text = lines[lineNo - 1];
104-
if (operator === '+') {
105-
lines[lineNo - 1] = `<span class="diff-insertion"><span class="diff-operator">+</span>${text}</span>`;
106-
} else {
107-
lines[lineNo - 1] = `<span class="diff-deletion"><span class="diff-operator">-</span>${text}</span>`;
108-
}
109-
});
110-
codeblock = lines.join('\n');
111-
}
112-
113118
codeblock = showdown.subParser('hashBlock')(codeblock, options, globals);
114119

115120
// Since GHCodeblocks can be false positives, we need to

tests/dummy/public/example.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,22 @@ Router.map(function() {
6262

6363
export default Router;
6464
```
65+
66+
With a diff on the first line:
67+
68+
```javascript {data-filename="app/router.js" data-diff="-1,+2"}
69+
import EmberRouter from '@ember/routing/router';
70+
import EmberRouter from '@embroider/router';
71+
import config from './config/environment';
72+
73+
const Router = EmberRouter.extend({
74+
location: config.locationType,
75+
rootURL: config.rootURL
76+
});
77+
78+
Router.map(function() {
79+
this.route('about');
80+
});
81+
82+
export default Router;
83+
```

0 commit comments

Comments
 (0)