Skip to content

Commit

Permalink
fix: Simplify underline handler
Browse files Browse the repository at this point in the history
  • Loading branch information
tripodsan committed Jan 19, 2023
1 parent b895ead commit 0aa1b14
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 125 deletions.
113 changes: 36 additions & 77 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"@adobe/remark-gridtables": "1.0.1",
"form-data": "4.0.0",
"fs-extra": "11.1.0",
"hast-util-to-html": "8.0.4",
"hast-util-to-mdast": "9.0.0",
"jsdom": "21.0.0",
"node-fetch": "3.3.0",
Expand Down
47 changes: 8 additions & 39 deletions src/importer/PageImporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { JSDOM } from 'jsdom';
import path from 'path';
import { unified } from 'unified';
import parse from 'rehype-parse';
import { toHtml } from 'hast-util-to-html';
import rehype2remark from 'rehype-remark';
import stringify from 'remark-stringify';
import fs from 'fs-extra';
Expand Down Expand Up @@ -66,47 +65,17 @@ export default class PageImporter {
.use(parse, { emitParseErrors: true })
.use(rehype2remark, {
handlers: {
hlxembed: (state, node) => {
const children = node.children.map((child) => processor.stringify(child).trim());
return {
type: 'paragraph',
children: [{
type: 'text',
value: children.join(''),
}],
};
},
u: (state, node) => {
if (node.children && node.children.length > 0) {
const children = node.children.map((child) => {
try {
if (child.type === 'element' && child.tagName !== 'span') {
const n = {
type: child.tagName,
children: child.children,
};
return processor.stringify(n).trim();
}
return processor.stringify(child).trim();
} catch (e) {
// cannot stringify the node, return html
return toHtml(child);
}
});
return {
return [{
type: 'html',
value: '<u>',
},
...state.all(node),
{
type: 'html',
value: `<u>${children.join('')}</u>`,
};
// todo: use this
// return [{
// type: 'html',
// value: '<u>',
// },
// ...state.all(node),
// {
// type: 'html',
// value: '</u>',
// }];
value: '</u>',
}];
}
return '';
},
Expand Down
1 change: 0 additions & 1 deletion test/importers/PageImporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ describe('PageImporter tests - fixtures', () => {

const md = await storageHandler.get(results[0].md);
const expectedMD = await fs.readFile(path.resolve(__dirname, 'fixtures', `${feature}.spec.md`), 'utf-8');
console.log(md);
strictEqual(md.trim(), expectedMD.trim(), 'imported md is expected one');
};

Expand Down
1 change: 0 additions & 1 deletion test/importers/fixtures/link.spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ <h1>Links sample</h1>
<p><a href="http://wwww.sample.com/a">Link on a single online</a></p>
<a href="http://wwww.sample.com/b">Link without p</a>
<p>http://wwww.sample.com/c</p>
<hlxembed>http://wwww.sample.com/d</hlxembed>
</body>
</html>
4 changes: 1 addition & 3 deletions test/importers/fixtures/link.spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

[Link without p](http://wwww.sample.com/b)

http://wwww.sample.com/c

http://wwww.sample.com/d
http://wwww.sample.com/c
12 changes: 9 additions & 3 deletions test/importers/fixtures/u.spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ Some normal text with random <u>underline</u> or <u>span with underline</u> or <

**<u>Underline 3</u>**

- <u>li underline 1</u>
- <u>li underline 2</u>
- <u>
li underline 1
</u>
- <u>
li underline 2
</u>
also may have text here
- <u>li underline 3</u>
- <u>
li underline 3
</u>

[Unlined link](https:/www.sample.com/a) or [<u>Linked underline</u>](https:/www.sample.com/b) ?

Expand Down

0 comments on commit 0aa1b14

Please sign in to comment.