mirror of
https://github.com/mindoc-org/mindoc.git
synced 2025-09-21 03:44:17 +08:00
1、实现富文本编辑器
2、实现项目排序
This commit is contained in:
76
static/to-markdown/lib/html-parser.js
Normal file
76
static/to-markdown/lib/html-parser.js
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Set up window for Node.js
|
||||
*/
|
||||
|
||||
var _window = (typeof window !== 'undefined' ? window : this)
|
||||
|
||||
/*
|
||||
* Parsing HTML strings
|
||||
*/
|
||||
|
||||
function canParseHtmlNatively () {
|
||||
var Parser = _window.DOMParser
|
||||
var canParse = false
|
||||
|
||||
// Adapted from https://gist.github.com/1129031
|
||||
// Firefox/Opera/IE throw errors on unsupported types
|
||||
try {
|
||||
// WebKit returns null on unsupported types
|
||||
if (new Parser().parseFromString('', 'text/html')) {
|
||||
canParse = true
|
||||
}
|
||||
} catch (e) {}
|
||||
|
||||
return canParse
|
||||
}
|
||||
|
||||
function createHtmlParser () {
|
||||
var Parser = function () {}
|
||||
|
||||
// For Node.js environments
|
||||
if (typeof document === 'undefined') {
|
||||
var jsdom = require('jsdom')
|
||||
Parser.prototype.parseFromString = function (string) {
|
||||
return jsdom.jsdom(string, {
|
||||
features: {
|
||||
FetchExternalResources: [],
|
||||
ProcessExternalResources: false
|
||||
}
|
||||
})
|
||||
}
|
||||
} else {
|
||||
if (!shouldUseActiveX()) {
|
||||
Parser.prototype.parseFromString = function (string) {
|
||||
var doc = document.implementation.createHTMLDocument('')
|
||||
doc.open()
|
||||
doc.write(string)
|
||||
doc.close()
|
||||
return doc
|
||||
}
|
||||
} else {
|
||||
Parser.prototype.parseFromString = function (string) {
|
||||
var doc = new window.ActiveXObject('htmlfile')
|
||||
doc.designMode = 'on' // disable on-page scripts
|
||||
doc.open()
|
||||
doc.write(string)
|
||||
doc.close()
|
||||
return doc
|
||||
}
|
||||
}
|
||||
}
|
||||
return Parser
|
||||
}
|
||||
|
||||
function shouldUseActiveX () {
|
||||
var useActiveX = false
|
||||
|
||||
try {
|
||||
document.implementation.createHTMLDocument('').open()
|
||||
} catch (e) {
|
||||
if (window.ActiveXObject) useActiveX = true
|
||||
}
|
||||
|
||||
return useActiveX
|
||||
}
|
||||
|
||||
module.exports = canParseHtmlNatively() ? _window.DOMParser : createHtmlParser()
|
Reference in New Issue
Block a user