Skip to content

Commit

Permalink
feat: replace weird spaces characters
Browse files Browse the repository at this point in the history
  • Loading branch information
kptdobe committed Apr 5, 2022
1 parent cbbadfb commit 4c9b43f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{
"type": "node",
"request": "launch",
"name": "Mocha Current File",
"name": "Mocha All",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion src/importer/PageImporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export default class PageImporter {
}

postProcessMD(md) {
return md.replace(/\\\\~/gm, '\\~');
return MDUtils.cleanupMarkdown(md);
}

async download(url) {
Expand Down
16 changes: 16 additions & 0 deletions src/utils/MDUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,20 @@ export default class MDUtils {
return md.replace(new RegExp(`${oldSrc.replace('.', '\\.').replace('?', '\\?')}`, 'gm'), newSrc);
}
};

static cleanupMarkdown(md) {
let ret = md?.replace(/\\\\~/gm, '\\~');
if (ret) {
for (let i = 0; i < 20; i += 1) {
let x = `${i}`;
if (i < 10) x = `0${i}`;
const c = String.fromCodePoint(parseInt(`00${x}`, 16));
const reg = new RegExp(`\\u00${x}`, 'g');
const r = [c.length].map(() => ' ').join('');
ret = ret.replace(reg, r);
}
ret = ret.replace(/\u00A0/gm, '');
}
return ret;
}
}
29 changes: 28 additions & 1 deletion test/utils/MDUtils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { describe, it } from 'mocha';

import MDUtils from '../../src/utils/MDUtils.js';

describe('MDUtils tests', () => {
describe('MDUtils#replaceSrcInMarkdown tests', () => {
it('MDUtils#replaceSrcInMarkdown do nothing', () => {
strictEqual(
MDUtils.replaceSrcInMarkdown('#A title\n![](https://www.sample.com/image.jpg)', 'https://www.sample.com/replace.jpg', 'https://www.sample.com/withimage.jpg'),
Expand Down Expand Up @@ -52,3 +52,30 @@ describe('MDUtils tests', () => {
);
});
});

describe('MDUtils#cleanupMarkdown tests', () => {
it('MDUtils#cleanupMarkdown do nothing', () => {
const md = '#A title\n![](https://www.sample.com/image.jpg)\nShould be clean\n<table><tr><th colspan="2">Metadata</th></tr><tr><td>key</td><td>value</td></tr></table>';
strictEqual(
MDUtils.cleanupMarkdown(md),
md,
'do nothing',
);
});

it('MDUtils#cleanupMarkdown unescape tildes', () => {
strictEqual(
MDUtils.cleanupMarkdown('#A title\n~~Tilde can be an pb~~\n Especially \\\\~in the content'),
'#A title\n~~Tilde can be an pb~~\n Especially \\~in the content',
'unescape tildes',
);
});

it('MDUtils#cleanupMarkdown replace weird spaces', () => {
strictEqual(
MDUtils.cleanupMarkdown('#A title\nReplaces the weird spaces characters: "\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019"'),
'#A title\nReplaces the weird spaces characters: " "',
'replace weird spaces',
);
});
});

0 comments on commit 4c9b43f

Please sign in to comment.