Skip to content

Commit

Permalink
fix: Whitespace between some nodes not properly handled (fixes #16)
Browse files Browse the repository at this point in the history
  • Loading branch information
nonara committed Jul 13, 2021
1 parent 19f3177 commit a7abf81
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"node": ">=10.0.0"
},
"dependencies": {
"node-html-parser": "^4.0.0"
"node-html-parser": "^4.1.0"
},
"devDependencies": {
"@types/jest": "^26.0.23",
Expand Down
2 changes: 1 addition & 1 deletion src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export function parseHTML(html: string, options: NodeHtmlMarkdownOptions): Eleme
}
} else nodeHtmlParse = getNodeHtmlParser();

if (!el) el = nodeHtmlParse!(html, nodeHtmlParserConfig).removeWhitespace();
if (!el) el = nodeHtmlParse!(html, nodeHtmlParserConfig);
perfStop('parse');

return el;
Expand Down
4 changes: 2 additions & 2 deletions src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ export class Visitor {
/* Handle text node */
if (isTextNode(node))
return node.isWhitespace
? void 0
: this.appendResult(metadata?.noEscape ? node.text : this.processText(node.text));
? (!result.text.length || result.trailingNewlineStats.whitespace > 0) ? void 0 : this.appendResult(' ')
: this.appendResult(metadata?.noEscape ? node.trimmedText : this.processText(node.trimmedText));

if (textOnly || !isElementNode(node)) return;

Expand Down
4 changes: 2 additions & 2 deletions test/default-tags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe(`Default Tags`, () => {
<a>a<strong>b</strong></a> <!-- This node is treated as text due to no href -->
<a href="${url}">${url}</a>
`);
expect(res).toBe(`[a b**c**](${url})a**b**<${url}>`);
expect(res).toBe(`[a b**c**](${url}) a**b** <${url}> `);
});

test(`Image (img)`, () => {
Expand All @@ -61,7 +61,7 @@ describe(`Default Tags`, () => {
<img src="${url}4" title="t4" alt="a4">
<img src="data:image/gif;base64,R/"><!-- This node is elided due to default option keepDataImages = false -->
`);
expect(res).toBe(`![](${url}1)` + `![](${url}3 "t3")` + `![a4](${url}4 "t4")`);
expect(res).toBe(`![](${url}1)` + ` ![](${url}3 "t3")` + ` ![a4](${url}4 "t4") `);
});

test(`Pre-formatted Text (pre)`, () => {
Expand Down
8 changes: 4 additions & 4 deletions test/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,12 @@ text`);
instance.options.keepDataImages = true;
const resKeep = translate(`<img alt="normal" src="normal_img.jpg">
<img src="data:image/gif;base64,R0lGODlhEA"/>`);
expect(resKeep).toBe(`![normal](normal_img.jpg)![](data:image/gif;base64,R0lGODlhEA)`);
expect(resKeep).toBe(`![normal](normal_img.jpg) ![](data:image/gif;base64,R0lGODlhEA)`);

instance.options.keepDataImages = false;
const resNoKeep = translate(`<img alt="normal" src="normal_img.jpg">
<img src="data:image/gif;base64,R0lGODlhEA"/>`);
expect(resNoKeep).toBe(`![normal](normal_img.jpg)`);
expect(resNoKeep).toBe(`![normal](normal_img.jpg) `);

instance.options.keepDataImages = originalKeepDataImages;
});
Expand All @@ -265,13 +265,13 @@ text`);
instance.options.useLinkReferenceDefinitions = false;
let res = translate(html);
expect(res).toBe(
`Hello: [a b**c**](${url})a**b**[link2](${url}/other)[repeat link](${url})<${url}> Goodbye!`
`Hello: [a b**c**](${url}) a**b** [link2](${url}/other) [repeat link](${url}) <${url}> Goodbye!`
);

instance.options.useLinkReferenceDefinitions = true;
res = translate(html);
expect(res).toBe(
`Hello: [a b**c**][1]a**b**[link2][2][repeat link][1]<${url}> Goodbye!\n\n[1]: ${url}\n[2]: ${url}/other`
`Hello: [a b**c**][1] a**b** [link2][2] [repeat link][1] <${url}> Goodbye!\n\n[1]: ${url}\n[2]: ${url}/other`
);

instance.options.useLinkReferenceDefinitions = originalUseLinkReferenceDefinitions;
Expand Down
8 changes: 7 additions & 1 deletion test/special-cases.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe(`Special Cases`, () => {
test.each(textFormatTags)(`%s`, tag => {
const delim = delims[tag];

expect(translate(`<p><${tag}> &nbsp;Label:&nbsp; </${tag}>Value</p>`)).toBe(` ${delim}Label:${delim} Value`);
expect(translate(`<p><${tag}> &nbsp;Label:&nbsp; </${tag}>Value</p>`)).toBe(` ${delim}Label:${delim} Value`);
expect(translate(`<p><${tag}>&nbsp; Label: &nbsp;</${tag}>Value</p>`)).toBe(` ${delim}Label:${delim} Value`);
});
});
Expand All @@ -62,4 +62,10 @@ describe(`Special Cases`, () => {
);
});
});

// See: https://github.com/crosstype/node-html-markdown/issues/16
test(`Handles whitespace with single space`, () => {
const res = translate(`<span>test</span> <span>test2 </span>\n<span>test3</span>\r\n\r\n\t\t\t<span>test4</span>`);
expect(res).toBe(`test test2 test3 test4`);
});
});
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2963,10 +2963,10 @@ neo-async@^2.6.0:
resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.2.tgz#b4aafb93e3aeb2d8174ca53cf163ab7d7308305f"
integrity sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==

node-html-parser@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/node-html-parser/-/node-html-parser-4.0.0.tgz#95e4fec010c48425821d5659eaf39e27b0781b6d"
integrity sha512-vuOcp3u4GrfYOcqe+FpUffQKnfVBq781MbtFEcTR6fJYnYRE2qUPdDaDk7TGdq6H9r2B3TbyU6K9Rah6/C7qvg==
node-html-parser@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/node-html-parser/-/node-html-parser-4.1.0.tgz#9c23130609e203030113255c519ff2d3fbbfc439"
integrity sha512-sqSaqyrg1mqsRTl0r0h4UVAHBoW605rSpanOz0Yr7NzERl0fJ1eh6CRoZFcIXEIyzD3QJfGqsIMP+kzkViTiAA==
dependencies:
css-select "^4.1.3"
he "1.2.0"
Expand Down

0 comments on commit a7abf81

Please sign in to comment.