mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 17:36:08 +08:00
修复一些小错误
This commit is contained in:
1 parent
8cd84f29e8
commit
a596231a06
15 files changed
+125
-124
No files matched your search
+24
-14
@@ -1,16 +1,26 @@
|
||||
global.Hydro.lib.html2md = module.exports = function html2md(str) {
|
||||
const RE_IMG = /<img.*?src="(.*?)".*?>(.*?)<\/img>/i;
|
||||
const RE_A = /<a.*?href="(.*?)".*?>(.*?)<\/a>/i;
|
||||
const RE_BLOCKQUOTE = /<blockquote.*?>(.*?)<\/blockquote>/i;
|
||||
|
||||
function replacer(RE, cb) { // eslint-disable-line no-unused-vars
|
||||
// eslint-disable-next-line no-eval
|
||||
return eval(`(substr)=>{
|
||||
const s = RE.exec(substr);
|
||||
return cb(s);
|
||||
}`);
|
||||
}
|
||||
|
||||
function html2md(str) {
|
||||
return str.replace(/<p>/gmi, '').replace(/<\/p>/gmi, ' ')
|
||||
.replace(/<h1>/gmi, '# ')
|
||||
.replace(/<h2>/gmi, '## ')
|
||||
.replace(/<h3>/gmi, '### ')
|
||||
.replace(/<h4>/gmi, '#### ')
|
||||
.replace(/<h5>/gmi, '##### ')
|
||||
.replace(/<h6>/gmi, '###### ')
|
||||
.replace(/<\/?code>/, '\n```\n')
|
||||
.replace(/<img.*?src=".*?".*?>.*?<\/img>/gmi, (substr) => {
|
||||
const RE_IMG = /<img.*?src="(.*?)".*?>(.*?)<\/img>/i;
|
||||
const res = RE_IMG.exec(substr);
|
||||
return `[${res[2]}](${res[1]})`;
|
||||
.replace(/<h[1-6]>/gmi, (substr) => {
|
||||
const s = /<h[1-6]>/i.exec(substr);
|
||||
return `${'#'.repeat(parseInt(s[1]))} `;
|
||||
})
|
||||
.replace(/<\/h[0-6]>/, '\n');
|
||||
};
|
||||
.replace(/<\/h[0-6]>/, '\n')
|
||||
.replace(/<\/?code>/, '\n```\n')
|
||||
.replace(/<img.*?src=".*?".*?>.*?<\/img>/gmi, replacer(RE_IMG, (s) => `![${s[2]}](${s[1]})`))
|
||||
.replace(/<a.*?href=".*?".*?>.*?<\/a>/gmi, replacer(RE_A, (s) => `[${s[2]}](${s[1]})`))
|
||||
.replace(/<blockquote.*?>.*?<\/blockquote>/gmi, replacer(RE_BLOCKQUOTE, (s) => `> ${s[1]}`));
|
||||
}
|
||||
|
||||
global.Hydro.lib.html2md = module.exports = html2md;
|
||||
Reference in new issue
Block a user