Skip to content

Commit

Permalink
feat: implement superscript and subscript support (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
kptdobe committed Apr 20, 2023
1 parent 3d514d6 commit 1f0d97f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
32 changes: 18 additions & 14 deletions src/importer/PageImporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ function remarkImageReferences() {
return imageReferences;
}

function htmlElementNode(element, state, node) {
if (node.children && node.children.length > 0) {
return [{
type: 'html',
value: `<${element}>`,
},
...state.all(node),
{
type: 'html',
value: `</${element}>`,
}];
}
return '';
}

export default class PageImporter {
params;

Expand Down Expand Up @@ -65,20 +80,9 @@ export default class PageImporter {
.use(parse, { emitParseErrors: true })
.use(rehype2remark, {
handlers: {
u: (state, node) => {
if (node.children && node.children.length > 0) {
return [{
type: 'html',
value: '<u>',
},
...state.all(node),
{
type: 'html',
value: '</u>',
}];
}
return '';
},
u: (state, node) => htmlElementNode('u', state, node),
sub: (state, node) => htmlElementNode('sub', state, node),
sup: (state, node) => htmlElementNode('sup', state, node),
...gridtableHandlers,
},
})
Expand Down
4 changes: 2 additions & 2 deletions test/importers/PageImporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ describe('PageImporter tests - fixtures', () => {
await featureTest('space');
});

it('import - strikethroughs', async () => {
await featureTest('s');
it('import - sub and sup', async () => {
await featureTest('subsup');
});
});
7 changes: 7 additions & 0 deletions test/importers/fixtures/subsup.spec.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<body>
<h1>sub and sup</h1>
<p>This text must be a <sub>subscript</sub>.</p>
<p>This text must be a <sup>superscript</sup>.</p>
</body>
</html>
5 changes: 5 additions & 0 deletions test/importers/fixtures/subsup.spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# sub and sup

This text must be a <sub>subscript</sub>.

This text must be a <sup>superscript</sup>.

0 comments on commit 1f0d97f

Please sign in to comment.