From 0220004287e4fcb33f5d715ce8d7750652b721c3 Mon Sep 17 00:00:00 2001 From: fry69 <142489379+fry69@users.noreply.github.com> Date: Tue, 22 Aug 2023 11:20:55 +0200 Subject: [PATCH 1/4] refactor diffInfo to function --- addon/initializers/showdown-extension.js | 39 +++++++++++++----------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/addon/initializers/showdown-extension.js b/addon/initializers/showdown-extension.js index 4bed4c3..d8c3201 100644 --- a/addon/initializers/showdown-extension.js +++ b/addon/initializers/showdown-extension.js @@ -6,6 +6,25 @@ import { assert } from '@ember/debug'; // taken from prismjs, regex to detect newlines in text const NEW_LINE_EXP = /\n(?!$)/g; +function diffInfo(args, codeblock) { + if (args) { + let lines = codeblock.split('\n'); + + args.forEach(pD => { + let operator = pD[0]; + let lineNo = +(pD.replace(operator, '')); + let text = lines[lineNo - 1]; + if (operator === '+') { + lines[lineNo - 1] = `+${text}`; + } else { + lines[lineNo - 1] = `-${text}`; + } + }); + codeblock = lines.join('\n'); + } + return codeblock; +} + function getLineNumbersHTML(index, codeblock) { let match = codeblock.match(NEW_LINE_EXP); let linesNum = match ? match.length + 1 : 1; @@ -79,6 +98,7 @@ export function initialize(/* application */) { codeblock = codeblock.replace(/¨T/g, '¨'); let highlightedCodeBlock = Prism.highlight(codeblock, Prism.languages[language], language) + end; + highlightedCodeBlock = diffInfo(attributes['data-diff']?.split(','), highlightedCodeBlock); codeblock = `
${highlightedCodeBlock}${lineNumbersHTML}
`; // Convert to the special characters Showdown uses again @@ -89,27 +109,10 @@ export function initialize(/* application */) { codeblock = `
${attributes['data-filename'] || ''}${codeblock}
`; } } else { + codeblock = diffInfo(attributes['data-diff']?.split(','), codeblock); codeblock = `
${codeblock}${lineNumbersHTML}
`; } - const diffInfo = attributes['data-diff']?.split(','); - - if (diffInfo) { - let lines = codeblock.split('\n'); - - diffInfo.forEach(pD => { - let operator = pD[0]; - let lineNo = +(pD.replace(operator, '')); - let text = lines[lineNo - 1]; - if (operator === '+') { - lines[lineNo - 1] = `+${text}`; - } else { - lines[lineNo - 1] = `-${text}`; - } - }); - codeblock = lines.join('\n'); - } - codeblock = showdown.subParser('hashBlock')(codeblock, options, globals); // Since GHCodeblocks can be false positives, we need to From ef0c7e1a303c2241d36d09c61b31ae22abcd8b67 Mon Sep 17 00:00:00 2001 From: fry69 <142489379+fry69@users.noreply.github.com> Date: Tue, 22 Aug 2023 12:31:17 +0200 Subject: [PATCH 2/4] Small visual code fixes --- addon/initializers/showdown-extension.js | 45 +++++++++++++----------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/addon/initializers/showdown-extension.js b/addon/initializers/showdown-extension.js index d8c3201..f4cb500 100644 --- a/addon/initializers/showdown-extension.js +++ b/addon/initializers/showdown-extension.js @@ -6,25 +6,6 @@ import { assert } from '@ember/debug'; // taken from prismjs, regex to detect newlines in text const NEW_LINE_EXP = /\n(?!$)/g; -function diffInfo(args, codeblock) { - if (args) { - let lines = codeblock.split('\n'); - - args.forEach(pD => { - let operator = pD[0]; - let lineNo = +(pD.replace(operator, '')); - let text = lines[lineNo - 1]; - if (operator === '+') { - lines[lineNo - 1] = `+${text}`; - } else { - lines[lineNo - 1] = `-${text}`; - } - }); - codeblock = lines.join('\n'); - } - return codeblock; -} - function getLineNumbersHTML(index, codeblock) { let match = codeblock.match(NEW_LINE_EXP); let linesNum = match ? match.length + 1 : 1; @@ -44,6 +25,25 @@ function stripQuotes(string) { return string; } +function diffInfo(args, codeblock) { + if (args) { + let lines = codeblock.split('\n'); + + args.forEach(pD => { + let operator = pD[0]; + let lineNo = +(pD.replace(operator, '')); + let text = lines[lineNo - 1]; + if (operator === '+') { + lines[lineNo - 1] = `+${text}`; + } else { + lines[lineNo - 1] = `-${text}`; + } + }); + codeblock = lines.join('\n'); + } + return codeblock; +} + export function initialize(/* application */) { showdown.subParser('githubCodeBlocks', function (text, options, globals) { // early exit if option is not enabled @@ -89,6 +89,8 @@ export function initialize(/* application */) { let lineNumbersHTML = getLineNumbersHTML(idCounter, codeblock); idCounter++; + let diffInfoArgs = attributes['data-diff']?.split(','); + assert(`Language "${language}" not found. Have you configured Prism correctly?`, !language || Prism.languages[language]); if (language && Prism.languages[language]) { @@ -98,7 +100,7 @@ export function initialize(/* application */) { codeblock = codeblock.replace(/¨T/g, '¨'); let highlightedCodeBlock = Prism.highlight(codeblock, Prism.languages[language], language) + end; - highlightedCodeBlock = diffInfo(attributes['data-diff']?.split(','), highlightedCodeBlock); + highlightedCodeBlock = diffInfo(diffInfoArgs, highlightedCodeBlock); codeblock = `
${highlightedCodeBlock}${lineNumbersHTML}
`; // Convert to the special characters Showdown uses again @@ -109,7 +111,8 @@ export function initialize(/* application */) { codeblock = `
${attributes['data-filename'] || ''}${codeblock}
`; } } else { - codeblock = diffInfo(attributes['data-diff']?.split(','), codeblock); + // (bug?) This clause may never be reached as the assert above fires in this case? + codeblock = diffInfo(diffInfoArgs, codeblock); codeblock = `
${codeblock}${lineNumbersHTML}
`; } From fc60d1d24cc6a906e75cfc7836395d3557bfd5eb Mon Sep 17 00:00:00 2001 From: fry69 <142489379+fry69@users.noreply.github.com> Date: Tue, 22 Aug 2023 13:07:25 +0200 Subject: [PATCH 3/4] add example for diff on first line --- tests/dummy/public/example.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/dummy/public/example.md b/tests/dummy/public/example.md index de46199..86366ec 100644 --- a/tests/dummy/public/example.md +++ b/tests/dummy/public/example.md @@ -62,3 +62,22 @@ Router.map(function() { export default Router; ``` + +With a diff on the first line: + +```javascript {data-filename="app/router.js" data-diff="-1,+2"} +import EmberRouter from '@ember/routing/router'; +import EmberRouter from '@embroider/router'; +import config from './config/environment'; + +const Router = EmberRouter.extend({ + location: config.locationType, + rootURL: config.rootURL +}); + +Router.map(function() { + this.route('about'); +}); + +export default Router; +``` From ea187dd57c5dc8d21342b08d87b2b68293c53ed4 Mon Sep 17 00:00:00 2001 From: fry69 <142489379+fry69@users.noreply.github.com> Date: Tue, 22 Aug 2023 16:22:09 +0200 Subject: [PATCH 4/4] remove overager comment --- addon/initializers/showdown-extension.js | 1 - 1 file changed, 1 deletion(-) diff --git a/addon/initializers/showdown-extension.js b/addon/initializers/showdown-extension.js index f4cb500..f178c6c 100644 --- a/addon/initializers/showdown-extension.js +++ b/addon/initializers/showdown-extension.js @@ -111,7 +111,6 @@ export function initialize(/* application */) { codeblock = `
${attributes['data-filename'] || ''}${codeblock}
`; } } else { - // (bug?) This clause may never be reached as the assert above fires in this case? codeblock = diffInfo(diffInfoArgs, codeblock); codeblock = `
${codeblock}${lineNumbersHTML}
`; }