From 0b0c34b0c6cc9eb5ba4b9d4d6028c09ca9332a4d Mon Sep 17 00:00:00 2001 From: undefined Date: Tue, 1 Sep 2020 23:43:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=BA=E7=83=88=E8=B0=B4=E8=B4=A3fps?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=86=85=E5=B5=8Ckatex-html=EF=BC=8Cdiv?= =?UTF-8?q?=E5=92=8Cstylesheet=E7=9A=84=E8=A1=8C=E4=B8=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/fps-importer/handler.ts | 12 +++++------- packages/fps-importer/package.json | 3 +-- packages/html2md/lib.ts | 17 +++++++++++++++++ packages/html2md/package.json | 22 ++++++++++++++++++++++ packages/html2md/tsconfig.json | 19 +++++++++++++++++++ tsconfig.json | 2 ++ 6 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 packages/html2md/lib.ts create mode 100644 packages/html2md/package.json create mode 100644 packages/html2md/tsconfig.json diff --git a/packages/fps-importer/handler.ts b/packages/fps-importer/handler.ts index 3a037713e..848d578f6 100644 --- a/packages/fps-importer/handler.ts +++ b/packages/fps-importer/handler.ts @@ -2,17 +2,15 @@ import path from 'path'; import os from 'os'; import fs from 'fs-extra'; -import TurndownService from 'turndown'; import AdmZip from 'adm-zip'; import xml2js from 'xml2js'; import { LocalProblemConfig } from 'hydrooj'; +import { convertHTML } from '@hydrooj/html2md'; import { Route, Handler, param, Types, } from 'hydrooj/dist/service/server'; import { PERM } from 'hydrooj/dist/model/builtin'; -const turndownService = new TurndownService(); - class FpsProblemImportHandler extends Handler { async get() { this.response.template = 'problem_import_fps.html'; @@ -28,19 +26,19 @@ class FpsProblemImportHandler extends Handler { if (p.description) { content.push( this.translate('problem.import.problem_description'), - p.description.map((i) => turndownService.turndown(i)).join('\n'), + p.description.map(convertHTML).join('\n'), ); } if (p.input) { content.push( this.translate('problem.import.input_format'), - p.input.map((i) => turndownService.turndown(i)).join('\n'), + p.input.map(convertHTML).join('\n'), ); } if (p.output) { content.push( this.translate('problem.import.output_format'), - p.output.map((i) => turndownService.turndown(i)).join('\n'), + p.output.map(convertHTML).join('\n'), ); } if (p.sample_input) { @@ -60,7 +58,7 @@ class FpsProblemImportHandler extends Handler { if (p.hint) { content.push( this.translate('problem.import.hint'), - p.hint.map((i) => turndownService.turndown(i)).join('\n'), + p.hint.map(convertHTML).join('\n'), ); } const config: LocalProblemConfig = { diff --git a/packages/fps-importer/package.json b/packages/fps-importer/package.json index 11b842a32..469253930 100644 --- a/packages/fps-importer/package.json +++ b/packages/fps-importer/package.json @@ -7,14 +7,13 @@ "author": "undefined ", "license": "AGPL-3.0-only", "dependencies": { + "@hydrooj/html2md": "^1.0.0", "adm-zip": "^0.4.16", "fs-extra": "^9.0.1", "hydrooj": "^2.12.22", - "turndown": "^6.0.0", "xml2js": "^0.4.23" }, "devDependencies": { - "@types/turndown": "^5.0.0", "@types/xml2js": "^0.4.5" }, "scripts": { diff --git a/packages/html2md/lib.ts b/packages/html2md/lib.ts new file mode 100644 index 000000000..ed0c82e00 --- /dev/null +++ b/packages/html2md/lib.ts @@ -0,0 +1,17 @@ +import 'hydrooj'; +import TurndownService from 'turndown'; +import { JSDOM } from 'jsdom'; + +const turndownService = new TurndownService(); + +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); + }); + return turndownService.turndown(DOM.serialize()); +} + +global.Hydro.lib.convertHTML = convertHTML; diff --git a/packages/html2md/package.json b/packages/html2md/package.json new file mode 100644 index 000000000..be8e0f3f8 --- /dev/null +++ b/packages/html2md/package.json @@ -0,0 +1,22 @@ +{ + "name": "@hydrooj/html2md", + "version": "1.0.0", + "description": "turn html into markdown", + "main": "package.json", + "repository": "https://github.com/hydro-dev/Hydro.git", + "author": "undefined ", + "license": "AGPL-3.0-only", + "dependencies": { + "hydrooj": "^2.12.22", + "jsdom": "^16.4.0", + "turndown": "^6.0.0" + }, + "devDependencies": { + "@types/jsdom": "^16.2.3", + "@types/turndown": "^5.0.0" + }, + "scripts": { + "build": "tsc", + "lint": "eslint . --ext ts --fix" + } +} diff --git a/packages/html2md/tsconfig.json b/packages/html2md/tsconfig.json new file mode 100644 index 000000000..aa7d73857 --- /dev/null +++ b/packages/html2md/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "target": "es2019", + "module": "commonjs", + "esModuleInterop": true, + "moduleResolution": "node", + "declaration": true, + "sourceMap": true, + "composite": true, + "strictBindCallApply": true, + "experimentalDecorators": true, + "outDir": ".", + "rootDir": "." + }, + "include": [ + "*.ts" + ], + "exclude": [] +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 279e1209f..65c42ebb3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,6 +14,8 @@ { "path": "packages/hydrooj" }, { "path": "packages/hydrojudge" }, { "path": "packages/geoip" }, + { "path": "packages/html2md" }, + { "path": "packages/fps-importer" }, { "path": "packages/login-with-github" }, { "path": "packages/login-with-google" }, { "path": "packages/login-with-osu" },