From aa86b0ba3a2be06cba9dc58e90e9d50d9bc67c87 Mon Sep 17 00:00:00 2001
From: Keisir <100614278+Keisir@users.noreply.github.com>
Date: Tue, 19 Mar 2024 20:28:39 +0100
Subject: [PATCH 1/2] fix(blockquotes): first line break without alerts not
working
fixes #7
---
src/index.ts | 10 ++++---
test/__snapshots__/index.spec.ts.snap | 22 +++++++++++++++
test/index.spec.ts | 39 +++++++++++++++++++++++++++
3 files changed, 68 insertions(+), 3 deletions(-)
diff --git a/src/index.ts b/src/index.ts
index dad40b3..ad372f9 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -41,14 +41,17 @@ const processNode =
// So we just need to addtionally check if the following one is a block.
// The legacy title variant is not affected since it checks an inline and does not care about the newline.
+ // If this is not a gfm alert then we would manipulate the original children and the next processor would not be able to handle the children correctly.
+ const paragraphChildrenCopy = [...paragraph.children]
+
// No addtional inlines can exist in this paragraph for the title...
- if (paragraph.children.length > 1) {
+ if (paragraphChildrenCopy.length > 1) {
// Unless it is an inline break, which can be transformed to from 2 spaces with a newline.
- if (paragraph.children.at(1)?.type == 'break') {
+ if (paragraphChildrenCopy.at(1)?.type == 'break') {
// When it is, we actually have already found the line break required by GitHub.
// So we just strip the additional `
` element.
// The title element will be removed later.
- paragraph.children.splice(1, 1)
+ paragraphChildrenCopy.splice(1, 1)
} else {
return
}
@@ -62,6 +65,7 @@ const processNode =
if (admonitionType) {
// Remove the text as the title
paragraph.children.shift()
+ paragraph.children.shift()
} else {
return
}
diff --git a/test/__snapshots__/index.spec.ts.snap b/test/__snapshots__/index.spec.ts.snap
index a22896c..470ddf3 100644
--- a/test/__snapshots__/index.spec.ts.snap
+++ b/test/__snapshots__/index.spec.ts.snap
@@ -120,3 +120,25 @@ exports[`GitHub beta blockquote-based admonitions with titles like [!NOTE] > sho
"
`;
+
+exports[`verify default behavior with plugin enabled > should transform a plain blockquote with line breaks (" " - note the two spaces) 1`] = `
+"
+" +`; + +exports[`verify default behavior with plugin enabled > should transform a plain blockquote with line breaks (\\) 1`] = ` +"Each time you increase the amount of code, your software grows exponentially more complicated.
+
+-- DHH, Getting Real
+" +`; + +exports[`verify default behavior with plugin enabled > should transform a plain blockquote with line breaks (\\) 2x 1`] = ` +"Each time you increase the amount of code, your software grows exponentially more complicated.
+
+-- DHH, Getting Real
+" +`; diff --git a/test/index.spec.ts b/test/index.spec.ts index 944dafe..cbcd723 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -224,3 +224,42 @@ describe('GitHub beta blockquote-based admonitions with titles like [!NOTE]', fu } }) }) + +describe('verify default behavior with plugin enabled', function () { + defineCase('should transform a plain blockquote with line breaks (\\)', { + input: `\ +> Each time you increase the amount of code, your software grows exponentially more complicated.\\ +> -- DHH, Getting Real +`, + assertions(html) { + const elem = selectAll('blockquote > p > br', parseDocument(html)) + expect(elem).to.lengthOf(1) + } + }) + + defineCase( + 'should transform a plain blockquote with line breaks (" " - note the two spaces)', + { + input: `\ +> Each time you increase the amount of code, your software grows exponentially more complicated. +> -- DHH, Getting Real +`, + assertions(html) { + const elem = selectAll('blockquote > p > br', parseDocument(html)) + expect(elem).to.lengthOf(1) + } + } + ) + + defineCase('should transform a plain blockquote with line breaks (\\) 2x', { + input: `\ +> Each time you increase the amount of code, your software grows exponentially more complicated.\\ +> -- DHH, Getting Real\\ +> -- DHH, Getting Real +`, + assertions(html) { + const elem = selectAll('blockquote > p > br', parseDocument(html)) + expect(elem).to.lengthOf(2) + } + }) +}) From 8fcb9d9a47c5c2984b16780a66c0d514b0d268b0 Mon Sep 17 00:00:00 2001 From: Keisir <100614278+Keisir@users.noreply.github.com> Date: Tue, 19 Mar 2024 20:57:24 +0100 Subject: [PATCH 2/2] refactor(blockquotes): cleanup code --- src/index.ts | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/src/index.ts b/src/index.ts index ad372f9..24eec6d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -41,34 +41,29 @@ const processNode = // So we just need to addtionally check if the following one is a block. // The legacy title variant is not affected since it checks an inline and does not care about the newline. - // If this is not a gfm alert then we would manipulate the original children and the next processor would not be able to handle the children correctly. - const paragraphChildrenCopy = [...paragraph.children] + // Considering the reason why the paragraph ends here, the following one should be a children of the blockquote, which means it is always a block. + // So no more check is required. + title = text.value + admonitionType = config.types[title] + + if (!admonitionType) { + return + } // No addtional inlines can exist in this paragraph for the title... - if (paragraphChildrenCopy.length > 1) { + if (paragraph.children.length > 1) { // Unless it is an inline break, which can be transformed to from 2 spaces with a newline. - if (paragraphChildrenCopy.at(1)?.type == 'break') { + if (paragraph.children.at(1)?.type == 'break') { // When it is, we actually have already found the line break required by GitHub. // So we just strip the additional `Each time you increase the amount of code, your software grows exponentially more complicated.
+
+-- DHH, Getting Real
+-- DHH, Getting Real