公告管理

This commit is contained in:
ChenXinXin committed 2024-05-20 15:32:43 +08:00
1 parent 374ce74e2d
commit c0621ff48b
15 files changed
+4641

No files matched your search

@@ -0,0 +1,616 @@
<script>
class ArticleMarkdownEngine {
static settings = Object.assign({
gfm: true,
toc: true,
tocm: false,
tocStartLevel: 1,
tocTitle: '目录',
tocDropdown: false,
tocContainer: '',
markdown: '',
markdownSourceCode: false,
htmlDecode: false,
autoLoadKaTeX: true,
pageBreak: true,
atLink: true, // for @link
emailLink: true, // for mail address auto link
tex: false,
taskList: false, // Github Flavored Markdown task lists
emoji: false,
flowChart: false,
sequenceDiagram: false,
previewCodeHighlight: true,
}, {
// 定制配置,从前端复制
htmlDecode: 'style,script,iframe', // you can filter tags decode
// toc : false,
tocm: true, // Using [TOCM]
// tocContainer : "#custom-toc-container", // 自定义 ToC 容器层
// gfm : false,
// tocDropdown : true,
// markdownSourceCode : true, // 是否保留 Markdown 源码,即是否删除保存源码的 Textarea 标签
emoji: true,
taskList: true,
tex: true, // 默认不解析
flowChart: true, // 默认不解析
sequenceDiagram: true, // 默认不解析
});
static regexs = {
atLink: /@(\w+)/g,
email: /(\w+)@(\w+)\.(\w+)\.?(\w+)?/g,
emailLink: /(mailto:)?([\w\.\_]+)@(\w+)\.(\w+)\.?(\w+)?/g,
// mark : /==([^\s][^\<\/code\>][\s\S]*?[^\s])==(?!=)/g,
mark: /(={2})([^\<\/code\>].+?)\1/g,
emoji: /:([A-Za-z\+-]+):/g,
emojiDatetime: /(\d{2}:\d{2}:\d{2})/g,
twemoji: /:(tw-([\w]+)-?(\w+)?):/g,
fontAwesome: /:(fa-([\w]+)(-(\w+)){0,}):/g,
editormdLogo: /:(editormd-logo-?(\w+)?):/g,
pageBreak: /^\[[=]{8,}\]$/,
};
static urls = {
atLinkBase: 'https://github.com/',
};
// Emoji graphics files url path
static emoji = {
path: 'http://www.emoji-cheat-sheet.com/graphics/emojis/',
ext: '.png',
};
// Twitter Emoji (Twemoji) graphics files url path
static twemoji = {
path: 'http://twemoji.maxcdn.com/36x36/',
ext: '.png',
};
static classPrefix = 'editormd-';
static classNames = {
tex: ArticleMarkdownEngine.classPrefix + 'tex',
};
/**
* 清除字符串两边的空格
* Clear the space of strings both sides.
*
* @param {String} str string
* @return {String} trimed string
*/
static trim = function(str) {
return (!String.prototype.trim) ? str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '') : str.trim();
};
static filterHTMLTags(html, filters) {
const expression = filters.split('|');
const filterTags = expression[0].split(',');
for (let i = 0, len = filterTags.length; i < len; i++) {
const tag = filterTags[i];
html = html.replace(new RegExp('\<\s*' + tag + '\s*([^\>]*)\>([^\>]*)\<\s*\/' + tag + '\s*\>', 'igm'), '');
}
return html;
}
/**
* 自定义marked的解析器
* Custom Marked renderer rules
*
* @param {Array} markdownToC 传入用于接收TOC的数组
* @param options
* @return {Renderer} markedRenderer 返回marked的Renderer自定义对象
*/
static markedRenderer(markdownToC, options) {
const markedRenderer = new marked.Renderer();
markdownToC = markdownToC || [];
const atLinkReg = ArticleMarkdownEngine.regexs.atLink;
const markReg = ArticleMarkdownEngine.regexs.mark;
const emojiReg = ArticleMarkdownEngine.regexs.emoji;
const twemojiReg = ArticleMarkdownEngine.regexs.twemoji;
const faIconReg = ArticleMarkdownEngine.regexs.fontAwesome;
const editormdLogoReg = ArticleMarkdownEngine.regexs.editormdLogo;
const pageBreakReg = ArticleMarkdownEngine.regexs.pageBreak;
// 增加引用样式解析规则
markedRenderer.blockquote = function($quote) {
let quoteBegin = '';
const ps = $quote.match(/<p\s*?>/i);
if (ps !== null) {
quoteBegin = ps[0];
$quote = $quote.substr(3);
}
let $class = 'default';
if ($quote.indexOf('i ') === 0) {
$class = 'info';
$quote = $quote.substr(1);
} else if ($quote.indexOf('w ') === 0) {
$class = 'warning';
$quote = $quote.substr(1);
} else if ($quote.indexOf('s ') === 0) {
$class = 'success';
$quote = $quote.substr(1);
} else if ($quote.indexOf('d ') === 0) {
$class = 'danger';
$quote = $quote.substr(1);
}
return '<blockquote class="' + $class + '">\n' + quoteBegin + $quote + '</blockquote>\n';
};
// marked 解析图片
markedRenderer.image = function(href, title, text) {
const attr = '';
const begin = '';
const end = '';
if (/^=(.*?)/.test(text)) {
// console.log(text)
switch (text) {
case '=video':
if (href.match(/^.+.(mp4|m4v|ogg|ogv|webm)$/)) {
return "<video src='" + href + "' controls='controls' preload width=500></video>";
}
break;
case '=audio':
if (href.match(/^.+.(mp3|wav|flac|m4a)$/)) {
return "<audio src='" + href + "' controls='controls'></audio>";
}
break;
case '=video_iframe':
const youtubeMatch = href.match(/\/\/(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))([\w|-]{11})(?:(?:[\?&]t=)(\S+))?/);
const youkuMatch = href.match(/\/\/v\.youku\.com\/v_show\/id_(\w+)=*\.html/);
const qqMatch = href.match(/\/\/v\.qq\.com\/x\/cover\/.*\/([^\/]+)\.html\??.*/);
const coubMatch = href.match(/(?:www\.|\/\/)coub\.com\/view\/(\w+)/);
const facebookMatch = href.match(/(?:www\.|\/\/)facebook\.com\/([^\/]+)\/videos\/([0-9]+)/);
const dailymotionMatch = href.match(/.+dailymotion.com\/(video|hub)\/(\w+)\?/);
const bilibiliMatch = href.match(/(?:www\.|\/\/)bilibili\.com\/video\/(\w+)/);
const tedMatch = href.match(/(?:www\.|\/\/)ted\.com\/talks\/(\w+)/);
if (youtubeMatch && youtubeMatch[1].length === 11) {
return `<iframe height=400 width=500 frameborder=0 allowfullscreen src="//www.youtube.com/embed/${youtubeMatch[1] + (youtubeMatch[2] ? '?start=' + youtubeMatch[2] : '')}"></iframe>`;
} else if (youkuMatch && youkuMatch[1]) {
return `<iframe height=400 width=500 frameborder=0 allowfullscreen src="//player.youku.com/embed/${youkuMatch[1]}"></iframe>`;
} else if (qqMatch && qqMatch[1]) {
return `<iframe height=400 width=500 frameborder=0 allowfullscreen src="https://v.qq.com/txp/iframe/player.html?vid=${qqMatch[1]}"></iframe>`;
} else if (coubMatch && coubMatch[1]) {
return `<iframe height=400 width=500 frameborder=0 allowfullscreen src="//coub.com/embed/${coubMatch[1]}?muted=false&autostart=false&originalSize=true&startWithHD=true"></iframe>`;
} else if (facebookMatch && facebookMatch[0]) {
return `<iframe height=400 width=500 frameborder=0 allowfullscreen src="https://www.facebook.com/plugins/video.php?href=${encodeURIComponent(facebookMatch[0])}"></iframe>`;
} else if (dailymotionMatch && dailymotionMatch[2]) {
return `<iframe height=400 width=500 frameborder=0 allowfullscreen src="https://www.dailymotion.com/embed/video/${dailymotionMatch[2]}"></iframe>`;
} else if (bilibiliMatch && bilibiliMatch[1]) {
return `<iframe height=400 width=500 frameborder=0 allowfullscreen src="//player.bilibili.com/player.html?bvid=${bilibiliMatch[1]}"></iframe>`;
} else if (tedMatch && tedMatch[1]) {
return `<iframe height=400 width=500 frameborder=0 allowfullscreen src="//embed.ted.com/talks/${tedMatch[1]}"></iframe>`;
}
break;
default:
break;
}
}
return begin + '<img src="' + href + '" title="' + title + '" alt="' + text + '" ' + attr + ' />' + end;
};
// marked emoji 解析
markedRenderer.emoji = function(text) {
text = text.replace(ArticleMarkdownEngine.regexs.emojiDatetime, function($1) {
return $1.replace(/:/g, '&#58;');
});
const matchs = text.match(emojiReg);
if (!matchs || !ArticleMarkdownEngine.settings.emoji) {
return text;
}
for (let i = 0, len = matchs.length; i < len; i++) {
if (matchs[i] === ':+1:') {
matchs[i] = ':\\+1:';
}
text = text.replace(new RegExp(matchs[i]), function($1, $2) {
const faMatchs = $1.match(faIconReg);
const name = $1.replace(/:/g, '');
if (faMatchs) {
for (let fa = 0, len1 = faMatchs.length; fa < len1; fa++) {
const faName = faMatchs[fa].replace(/:/g, '');
return '<i class="fa ' + faName + ' fa-emoji" title="' + faName.replace('fa-', '') + '"></i>';
}
} else {
const emdlogoMathcs = $1.match(editormdLogoReg);
const twemojiMatchs = $1.match(twemojiReg);
if (emdlogoMathcs) {
for (let x = 0, len2 = emdlogoMathcs.length; x < len2; x++) {
const logoName = emdlogoMathcs[x].replace(/:/g, '');
return '<i class="' + logoName + '" title="Editor.md logo (' + logoName + ')"></i>';
}
} else if (twemojiMatchs) {
for (let t = 0, len3 = twemojiMatchs.length; t < len3; t++) {
const twe = twemojiMatchs[t].replace(/:/g, '').replace('tw-', '');
return '<img src="' + ArticleMarkdownEngine.twemoji.path + twe + ArticleMarkdownEngine.twemoji.ext + '" title="twemoji-' + twe + '" alt="twemoji-' + twe + '" class="emoji twemoji" />';
}
} else {
let src = (name === '+1') ? 'plus1' : name;
src = (src === 'black_large_square') ? 'black_square' : src;
src = (src === 'moon') ? 'waxing_gibbous_moon' : src;
return '<img src="' + ArticleMarkdownEngine.emoji.path + src + ArticleMarkdownEngine.emoji.ext + '" class="emoji" title="&#58;' + name + '&#58;" alt="&#58;' + name + '&#58;" />';
}
}
});
}
return text;
};
// marked @ 解析
markedRenderer.atLink = function(text) {
return text;
};
// marked 高亮标记解析
markedRenderer.mark = function(text) {
if (markReg.test(text)) {
// console.log(1,text)
const mark_replace_reg = /==(.+)==/g;
text = text.replace(markReg, function(e) {
// console.log(2,e)
return '<mark>' + e.replace(mark_replace_reg, function($1, $2) { return $2; }) + '</mark>';
});
}
return text;
};
// marked 链接解析
markedRenderer.link = function(href, title, text) {
if (this.options.sanitize) {
let prot;
try {
prot = decodeURIComponent(unescape(href)).replace(/[^\w:]/g, '').toLowerCase();
} catch (e) {
return '';
}
if (prot.indexOf('javascript:') === 0) {
return '';
}
}
let out = '<a href="' + href + '"';
if (atLinkReg.test(title) || atLinkReg.test(text)) {
if (title) {
out += ' title="' + title.replace(/@/g, '&#64;');
}
return out + '">' + text.replace(/@/g, '&#64;') + '</a>';
}
if (title) {
out += ' title="' + title + '"';
}
out += '>' + text + '</a>';
return out;
};
// marked 解析标题
markedRenderer.heading = function(text, level, raw) {
const linkText = text;
const hasLinkReg = /\s*\<a\s*href\=\"(.*)\"\s*([^\>]*)\>(.*)\<\/a\>\s*/;
const getLinkTextReg = /\s*\<a\s*([^\>]+)\>([^\>]*)\<\/a\>\s*/g;
if (hasLinkReg.test(text)) {
const tempText = [];
text = text.split(/\<a\s*([^\>]+)\>([^\>]*)\<\/a\>/);
for (let i = 0, len = text.length; i < len; i++) {
tempText.push(text[i].replace(/\s*href\=\"(.*)\"\s*/g, ''));
}
text = tempText.join(' ');
}
text = ArticleMarkdownEngine.trim(text);
const isChinese = /^[\u4e00-\u9fa5]+$/.test(text);
// var id = (isChinese) ? escape(text).replace(/\%/g, "") : text.toLowerCase().replace(/[^\w]+/g, "-");
const id = escape(text.toLowerCase().replace(' ', '')).replace(/\%/g, ''); // 生成包含中文编码的标题ID
const escapedText = text.toLowerCase().replace(/[^\w]+/g, '-');
const toc = {
text,
level,
slug: escapedText,
id: 'h' + level + '-' + this.options.headerPrefix + id,
};
markdownToC.push(toc);
let headingHTML = '<h' + level + ' id="h' + level + '-' + this.options.headerPrefix + id + '">';
// headingHTML += "<a name=\"" + text + "\" class=\"reference-link\"></a>";
headingHTML += '<a name="' + text.replace(/<[^>]*>\s?/g, '') + '" class="reference-link"></a>';
headingHTML += '<span class="header-link octicon octicon-link"></span>';
headingHTML += (hasLinkReg) ? this.atLink(this.mark(this.emoji(linkText))) : this.mark(this.emoji(text));
headingHTML += '</h' + level + '>';
// console.log(headingHTML)
return headingHTML;
};
// marked 解析分页符
markedRenderer.pageBreak = function(text) {
if (pageBreakReg.test(text) && ArticleMarkdownEngine.settings.pageBreak) {
text = '<hr style="page-break-after:always;" class="page-break editormd-page-break" />';
}
return text;
};
// marked 解析段落
markedRenderer.paragraph = function(text) {
const isTeXInline = /\$\$(.*)\$\$/g.test(text);
const isTeXLine = /^\$\$(.*)\$\$$/.test(text);
const isTeXAddClass = (isTeXLine) ? ' class="' + ArticleMarkdownEngine.classNames.tex + '"' : '';
const isToC = (ArticleMarkdownEngine.settings.tocm) ? /^(\[TOC\]|\[TOCM\])$/.test(text) : /^\[TOC\]$/.test(text);
const isToCMenu = /^\[TOCM\]$/.test(text);
if (!isTeXLine && isTeXInline) {
text = text.replace(/(\$\$([^\$]*)\$\$)+/g, function($1, $2) {
return '<span class="' + ArticleMarkdownEngine.classNames.tex + '">' + $2.replace(/\$/g, '') + '</span>';
});
} else {
text = (isTeXLine) ? text.replace(/\$/g, '') : text;
}
const tocHTML = '<div class="markdown-toc editormd-markdown-toc">' + text + '</div>';
return (isToC) ? ((isToCMenu) ? '<div class="editormd-toc-menu">' + tocHTML + '</div><br/>' : tocHTML)
: ((pageBreakReg.test(text)) ? this.pageBreak(text) : '<p' + isTeXAddClass + '>' + this.atLink(this.mark(this.emoji(text))) + '</p>\n');
};
// marked 解析代码块
markedRenderer.code = function(code, lang, escaped) {
// 自定义颜色function
function custom_color(color) {
if (color === 'red') {
return 'layui-bg-red';
} else if (color === 'orange') {
return 'layui-bg-orange';
} else if (color === 'green') {
return 'layui-bg-green';
} else if (color === 'cyan') {
return 'layui-bg-cyan';
} else if (color === 'black') {
return 'layui-bg-black';
} else if (color === 'grey') {
return 'layui-bg-grey';
}
return '';
}
if (lang === 'seq' || lang === 'sequence') {
return '<div class="sequence-diagram">' + code + '</div>';
} else if (lang === 'flow') {
return '<div class="flowchart">' + code + '</div>';
} else if (lang === 'math' || lang === 'latex' || lang === 'katex') {
return '<p class="' + editormd.classNames.tex + '">' + code + '</p>';
} else if (/^mindmap/i.test(lang)) { // 思维导图
const len = 9 || 32;
const $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
const maxPos = $chars.length;
let map_id = '';
for (let i = 0; i < len; i++) {
map_id += $chars.charAt(Math.floor(Math.random() * maxPos));
}
// var map_id = lang.split('>')[1];
// console.log(map_id)
let custom_height;
const h = lang.split('>')[1];
if (h != undefined) {
custom_height = h;
} else {
custom_height = 150;
}
return "<svg class='mindmap' style='width:100%;min-height:150px;height:" + custom_height + "px;' id='mindmap-" + map_id + "'>" + code + '</svg>';
} else if (/^echart/i.test(lang) || /^echarts/i.test(lang)) { // echart 图表
const len = 9 || 32;
const $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
const maxPos = $chars.length;
let map_id = '';
for (let i = 0; i < len; i++) {
map_id += $chars.charAt(Math.floor(Math.random() * maxPos));
}
// var map_id = lang.split('>')[1];
// console.log(map_id)
let custom_height;
const h = lang.split('>')[1];
if (h != undefined) {
custom_height = h;
} else {
custom_height = 150;
}
return "<div class='echart' style='width:100%;min-height:350px;height:" + custom_height + "px;' id='echart-" + map_id + "'>" + code + '</div>';
} else if (/^timeline/i.test(lang)) { // 时间线
let time_line = '<ul class="layui-timeline">';
// console.log(code)
const timeline_code = code.split(/(\r\n\t|\n|\r\t)+/);
// console.log(timeline_code)
timeline_code.forEach(function(item, index) {
// console.log(item,index)
if (item.match(/^# /)) { // 时间标题
time_line += '<li class="layui-timeline-item" style="list-style:none;">';
time_line += '<i class="layui-icon layui-timeline-axis">&#xe63f;</i>';
time_line += '<div class="layui-timeline-content layui-text">';
time_line += '<h3 class="layui-timeline-title" style="margin-top:0px;">';
time_line += item.replace('# ', '');
time_line += '</h3>';
} else if (/^[=]{4,}$/.test(item)) {
time_line += '</div></li>';
} else {
time_line += marked(item);
}
});
time_line += '</ul>';
return time_line;
} else if (/^title/i.test(lang)) { // site-title
let site_title = '<div class="site-title" style="margin:30px 0 20px;">';
// console.log(code)
const title_code = code.split(/[(\r\n)\r\n]+/);
// console.log(title_code)
title_code.forEach(function(item, index) {
// console.log(item,index)
if (item.match(/^# /)) { // 时间标题
site_title += '<fieldset style="border: none;padding: 0;border-top: 1px solid #eee;">';
site_title += '<legend style="margin-left: 20px; padding: 0 10px; font-size: 22px; font-weight: 300;">';
site_title += '<div name="default">';
site_title += item.replace('# ', '');
site_title += '</div></legend></fieldset>';
} else {
site_title += marked(item);
}
});
site_title += '</div>';
return site_title;
} else if (/^tblock/i.test(lang)) { // site-title-block
let title_block = '<div class="site-text" style="position: relative;">';
// console.log(code)
const titleblock_code = code.split(/[(\r\n)\r\n]+/);
// console.log(titleblock_code)
titleblock_code.forEach(function(item, index) {
// console.log(item,index)
if (item.match(/^# /)) { // 时间标题
title_block += '<fieldset class="layui-elem-field">';
title_block += '<legend>';
title_block += item.replace('# ', '');
title_block += '</div></legend><div class="layui-field-box">';
} else {
title_block += marked(item);
}
});
title_block += '</fieldset></div>';
return title_block;
} else if (/^hr/i.test(lang)) { // 分割线
const color = lang.split(' ')[1];
const ccolor = custom_color(color);
let hr = '<hr class="' + ccolor + '">';
hr += '</hr>';
return hr;
} else if (/^card/i.test(lang)) { // 卡片面板
const color = lang.split(' ')[1];
const ccolor = custom_color(color);
let card = '<div class="layui-card">';
// console.log(code)
const card_code = code.split(/[(\r\n)\r\n]+/);
// console.log(card_code)
card_code.forEach(function(item, index) {
// console.log(item,index)
if (item.match(/^# /)) { // 时间标题
card += '<div class="layui-card-header ' + ccolor + '">';
card += item.replace('# ', '');
card += '</div>';
card += '<div class="layui-card-body">';
} else {
card += marked(item);
}
});
card += '</div>';
return card;
}
return marked.Renderer.prototype.code.apply(this, arguments);
};
// marked 解析表格
markedRenderer.tablecell = function(content, flags) {
const type = (flags.header) ? 'th' : 'td';
const tag = (flags.align) ? '<' + type + ' style="text-align:' + flags.align + '">' : '<' + type + '>';
return tag + this.atLink(this.emoji(content)) + '</' + type + '>\n';
};
// marked 解析列表项
markedRenderer.listitem = function(text) {
if (ArticleMarkdownEngine.settings.taskList && /^\s*\[[x\s]\]\s*/.test(text)) {
text = text.replace(/^\s*\[\s\]\s*/, '<input type="checkbox" class="task-list-item-checkbox" /> ')
.replace(/^\s*\[x\]\s*/, '<input type="checkbox" class="task-list-item-checkbox" checked disabled /> ');
return '<li style="list-style: none;">' + this.atLink(this.emoji(text)) + '</li>';
}
return '<li>' + this.atLink(this.emoji(text)) + '</li>';
};
return markedRenderer;
}
static md2html(markdownDoc) {
// let markdownDoc = (ArticleMarkdownEngine.settings.markdown === '') ? saveTo.val() : ArticleMarkdownEngine.settings.markdown;
const markdownToC = [];
const rendererOptions = {
toc: ArticleMarkdownEngine.settings.toc,
tocm: ArticleMarkdownEngine.settings.tocm,
tocStartLevel: ArticleMarkdownEngine.settings.tocStartLevel,
taskList: ArticleMarkdownEngine.settings.taskList,
emoji: ArticleMarkdownEngine.settings.emoji,
tex: ArticleMarkdownEngine.settings.tex,
pageBreak: ArticleMarkdownEngine.settings.pageBreak,
atLink: ArticleMarkdownEngine.settings.atLink, // for @link
emailLink: ArticleMarkdownEngine.settings.emailLink, // for mail address auto link
flowChart: ArticleMarkdownEngine.settings.flowChart,
sequenceDiagram: ArticleMarkdownEngine.settings.sequenceDiagram,
previewCodeHighlight: ArticleMarkdownEngine.settings.previewCodeHighlight,
};
const markedOptions = {
renderer: this.markedRenderer(markdownToC, rendererOptions),
gfm: ArticleMarkdownEngine.settings.gfm,
tables: true,
breaks: true,
pedantic: false,
// MODIFY:允许 html tag,不进行转义
sanitize: false,
smartLists: true,
// MODIFY:不转义引号
smartypants: false,
};
marked.setOptions(markedOptions);
markdownDoc = markdownDoc.replace(/&nbsp;/g, ' ');
markdownDoc = markdownDoc.replace(/&gt;/g, '>');
let markdownParsed = marked.parse(markdownDoc);
markdownParsed = this.filterHTMLTags(markdownParsed, ArticleMarkdownEngine.settings.htmlDecode);
return markdownParsed;
}
}
</script>