2020-04-28 17:23:34 +08:00
|
|
|
|
import { bytesToSize, getUrl, getUrl2 } from 'educoder'
|
|
|
|
|
|
import showdown from 'showdown'
|
|
|
|
|
|
const $ = window.$
|
|
|
|
|
|
|
|
|
|
|
|
export function isImageExtension(fileName) {
|
|
|
|
|
|
return fileName ? !!(fileName.match(/.(jpg|jpeg|png|gif)$/i)) : false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function markdownToHTML(oldContent, selector) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
window.$('#md_div').html('')
|
|
|
|
|
|
if (selector && oldContent && oldContent.startsWith('<p')) { // 普通html处理
|
|
|
|
|
|
window.$('#' + selector).addClass('renderAsHtml')
|
|
|
|
|
|
window.$('#' + selector).html(oldContent)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
try {
|
|
|
|
|
|
$("#" + selector).html('')
|
|
|
|
|
|
// selector ||
|
|
|
|
|
|
var markdwonParser = window.editormd.markdownToHTML(selector || "md_div", {
|
|
|
|
|
|
markdown: oldContent, // .replace(/▁/g,"▁▁▁"),
|
|
|
|
|
|
emoji: true,
|
|
|
|
|
|
htmlDecode: "style,script,iframe", // you can filter tags decode
|
|
|
|
|
|
taskList: true,
|
|
|
|
|
|
tex: true, // 默认不解析
|
|
|
|
|
|
flowChart: true, // 默认不解析
|
|
|
|
|
|
sequenceDiagram: true // 默认不解析
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error(e)
|
|
|
|
|
|
}
|
|
|
|
|
|
// selector = '.' + selector
|
|
|
|
|
|
if (selector) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const content = window.$('#md_div').html()
|
|
|
|
|
|
if (selector) {
|
|
|
|
|
|
window.$(selector).html(content)
|
|
|
|
|
|
}
|
|
|
|
|
|
return content
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
function _doDownload(options) {
|
|
|
|
|
|
$.fileDownload(getUrl() + "/api" + options.url, {
|
|
|
|
|
|
successCallback: options.successCallback,
|
|
|
|
|
|
failCallback: options.failCallback
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
export function downloadFile(options) {
|
|
|
|
|
|
if ($.fileDownload) {
|
|
|
|
|
|
_doDownload(options)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const _url_origin = getUrl2()
|
|
|
|
|
|
$.getScript(
|
|
|
|
|
|
`${_url_origin}/javascripts/download/jquery.fileDownload.min.js`,
|
|
|
|
|
|
(data, textStatus, jqxhr) => {
|
|
|
|
|
|
_doDownload(options)
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function appendFileSizeToUploadFile(item) {
|
|
|
|
|
|
return `${item.title}${uploadNameSizeSeperator}${item.filesize}`
|
|
|
|
|
|
}
|
|
|
|
|
|
export function appendFileSizeToUploadFileAll(fileList) {
|
|
|
|
|
|
return fileList.map(item => {
|
|
|
|
|
|
if (item.name.indexOf(uploadNameSizeSeperator) == -1) {
|
|
|
|
|
|
return Object.assign({}, item, { name: `${item.name}${uploadNameSizeSeperator}${bytesToSize(item.size)}` })
|
|
|
|
|
|
}
|
|
|
|
|
|
return item
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
export const uploadNameSizeSeperator = ' '
|
|
|
|
|
|
|
|
|
|
|
|
export const sortDirections = ["ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend",
|
|
|
|
|
|
"ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend",
|
|
|
|
|
|
"ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend",
|
|
|
|
|
|
"ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend", "ascend", "descend",]
|
|
|
|
|
|
|
|
|
|
|
|
export function validateLength(str = '', length = 100) {
|
|
|
|
|
|
let len = 0
|
|
|
|
|
|
if (str) {
|
|
|
|
|
|
len = str.length
|
|
|
|
|
|
for (let i = 0; i < len; i++) {
|
|
|
|
|
|
let charCode = str.charCodeAt(i)
|
|
|
|
|
|
if (charCode >= 0xD800 && charCode <= 0xDBFF) {
|
|
|
|
|
|
len--
|
|
|
|
|
|
i++
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return len <= length
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function mdJSONParse(str) {
|
|
|
|
|
|
let val = "";
|
|
|
|
|
|
try {
|
|
|
|
|
|
return val = JSON.parse(str)
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
if (str) {
|
|
|
|
|
|
return val = str;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return "";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
showdown.extension('prettify', function () {
|
|
|
|
|
|
return [{
|
|
|
|
|
|
type: 'output',
|
|
|
|
|
|
filter: function (source) {
|
|
|
|
|
|
return source.replace(/(<pre[^>]*>)?[\n\s]?<code([^>]*)>/gi, function (match, pre, codeClass) {
|
|
|
|
|
|
if (pre) {
|
|
|
|
|
|
return '<pre class="prettyprint linenums"><code' + codeClass + '>'
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return ' <code class="prettyprint">'
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}]
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const showdownKatex = require('showdown-katex')
|
|
|
|
|
|
const CONVERTER = new showdown.Converter({
|
|
|
|
|
|
tables: true,
|
|
|
|
|
|
simpleLineBreaks: false,
|
|
|
|
|
|
underline: true,
|
|
|
|
|
|
extensions: [
|
|
|
|
|
|
'prettify',
|
|
|
|
|
|
showdownKatex({
|
|
|
|
|
|
throwOnError: false,
|
|
|
|
|
|
displayMode: false,
|
|
|
|
|
|
errorColor: '#1500ff',
|
|
|
|
|
|
}),
|
|
|
|
|
|
]
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const oldKatexMode = /(?:`?\$\$)([^`\$]+)(?:\$\$`?)/g
|
|
|
|
|
|
|
|
|
|
|
|
export function exportMdtoHtml(md = '') {
|
|
|
|
|
|
if (typeof md === 'string') {
|
|
|
|
|
|
let rs = md.replace(oldKatexMode, (_, p1) => {
|
|
|
|
|
|
return "$$" + p1 + "$$"
|
|
|
|
|
|
})
|
|
|
|
|
|
return CONVERTER.makeHtml(rs)
|
|
|
|
|
|
}
|
|
|
|
|
|
return md + ''
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function escapeText(text) {
|
|
|
|
|
|
return text.replace(/[&<>"']/g, s => {
|
|
|
|
|
|
const entityMap = {
|
|
|
|
|
|
'&': '&',
|
|
|
|
|
|
'<': '<',
|
|
|
|
|
|
'>': '>',
|
|
|
|
|
|
'"': '',
|
|
|
|
|
|
"'": '',
|
|
|
|
|
|
};
|
|
|
|
|
|
return entityMap[s]
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|