添加html支持

This commit is contained in:
undefined committed 2020-09-02 13:32:47 +08:00
1 parent 0b0c34b0c6
commit 3f2d98534f
9 files changed
+73 -20

No files matched your search

+9 -3
View File
@@ -2,14 +2,20 @@ import 'hydrooj';
import TurndownService from 'turndown';
import { JSDOM } from 'jsdom';
const turndownService = new TurndownService();
const turndownService = new TurndownService({
headingStyle: 'atx',
codeBlockStyle: 'fenced',
bulletListMarker: '-',
});
export function convertHTML(html: string) {
const DOM = new JSDOM(html);
const eles = DOM.window.document.querySelectorAll('span.katex-mathml');
eles.forEach((ele) => {
const MathML = ele.innerHTML;
ele.parentElement.replaceWith(MathML);
const MathML = ele.innerHTML
.replace(/\\{/gmi, '{')
.replace(/\\}/gmi, '}');
ele.parentElement.replaceWith(`$${MathML}$`);
});
return turndownService.turndown(DOM.serialize());
}
+3 -2
View File
@@ -2,7 +2,8 @@
"name": "@hydrooj/html2md",
"version": "1.0.0",
"description": "turn html into markdown",
"main": "package.json",
"main": "lib.js",
"typings": "lib.d.ts",
"repository": "https://github.com/hydro-dev/Hydro.git",
"author": "undefined <masnn0@outlook.com>",
"license": "AGPL-3.0-only",
@@ -19,4 +20,4 @@
"build": "tsc",
"lint": "eslint . --ext ts --fix"
}
}
}
+44
View File
@@ -0,0 +1,44 @@
import 'hydrooj';
import { convertHTML } from '@hydrooj/html2md/lib';
describe('html2md', () => {
const Katex1 = `\
<span class="katex">
<span class="katex-mathml">nn</span>
<span class="katex-html">
<span class="strut"></span>
<span class="strut bottom" style="vertical-align:0em;"></span>
<span class="base textstyle uncramped">
<span class="mord mathit">n</span>
</span>
</span>
</span>`;
const Katex2 = `\
<span class="katex">
<span class="katex-mathml">E={1,2,..,n}E=\\{1,2,..,n\\}</span>
<span class="katex-html">
<span class="strut"></span>
<span class="strut bottom" style="vertical-align:-0.25em;"></span>
<span class="base textstyle uncramped">
<span class="mord mathit">E</span>
<span class="mrel">=</span>
<span class="mopen">{</span>
<span class="mord mathrm">1</span>
<span class="mpunct">,</span>
<span class="mord mathrm">2</span>
<span class="mpunct">,</span>
<span class="mord mathrm">.</span>
<span class="mord mathrm">.</span>
<span class="mpunct">,</span>
<span class="mord mathit">n</span>
<span class="mclose">}</span>
</span>
</span>
</span>`;
test('convertHTML', () => {
expect(convertHTML('<h1>Test</h1><p>test</p>')).toStrictEqual('# Test\n\ntest');
expect(convertHTML(Katex1)).toStrictEqual('$n$');
expect(convertHTML(Katex2)).toStrictEqual('$E={1,2,..,n}$');
});
});