diff --git a/_config.yml b/_config.yml index 8704a6d..74f95f2 100755 --- a/_config.yml +++ b/_config.yml @@ -305,6 +305,7 @@ plugins: fcircle: /js/plugins/fcircle.js weibo: /js/plugins/weibo.js memos: /js/plugins/memos.js + marked: /js/plugins/marked.js marked: https://cdn.bootcdn.net/ajax/libs/marked/4.0.18/marked.min.js diff --git a/scripts/tags/index.js b/scripts/tags/index.js index cd13324..f714280 100644 --- a/scripts/tags/index.js +++ b/scripts/tags/index.js @@ -22,6 +22,7 @@ hexo.extend.tag.register('sites', require('./lib/sites')(hexo)) hexo.extend.tag.register('ghcard', require('./lib/ghcard')(hexo)) hexo.extend.tag.register('toc', require('./lib/toc')(hexo)) hexo.extend.tag.register('timeline', require('./lib/timeline')(hexo), {ends: true}) +hexo.extend.tag.register('md', require('./lib/md')(hexo)) // express hexo.extend.tag.register('checkbox', require('./lib/checkbox')(hexo, 'checkbox')) diff --git a/scripts/tags/lib/md.js b/scripts/tags/lib/md.js new file mode 100644 index 0000000..9453ca5 --- /dev/null +++ b/scripts/tags/lib/md.js @@ -0,0 +1,20 @@ +/** + * md.js v1.0 | https://github.com/volantis-x/hexo-theme-volantis/ + * contributor: @MHuiG + * + * 格式与官方标签插件一致使用空格分隔,中括号内的是可选参数(中括号不需要写出来) + * + * {% md [src:url] %} + * + */ +'use strict' + +var md_index = 0 + +module.exports = ctx => function(args) { + args = ctx.args.map(args, ['src']) + const md_id = "md_" + ++md_index + return ` +
+ ` +} \ No newline at end of file diff --git a/source/css/_layout/md.styl b/source/css/_layout/md.styl index cfedefe..6ad621b 100644 --- a/source/css/_layout/md.styl +++ b/source/css/_layout/md.styl @@ -161,3 +161,6 @@ h1.article-title bottom: 0 left: -2px right: -2px + &:has(img) + &:before + content: none diff --git a/source/js/main.js b/source/js/main.js index c1755ca..ede1020 100644 --- a/source/js/main.js +++ b/source/js/main.js @@ -242,7 +242,7 @@ if (stellar.plugins.stellar) { const els = document.getElementsByClassName('stellar-' + key + '-api'); if (els != undefined && els.length > 0) { stellar.jQuery(() => { - if (key == 'timeline' || 'memos') { + if (key == 'timeline' || 'memos' || 'marked') { stellar.loadScript(stellar.plugins.marked).then(function () { stellar.loadScript(js, { defer: true }); }); diff --git a/source/js/plugins/marked.js b/source/js/plugins/marked.js new file mode 100644 index 0000000..0bc3c3b --- /dev/null +++ b/source/js/plugins/marked.js @@ -0,0 +1,50 @@ +const loadMarkdown = (cfg) => { + if (!window.fetch) { + cfg.el.innerHTML = + '

Your browser outdated. Please use the latest version of Chrome or Firefox!

您的浏览器版本过低,请使用最新版的 Chrome 或 Firefox 浏览器!

'; + } else { + cfg.el.innerHTML = + '
'; + fetch(cfg.src, { method: "GET" }) + .then((resp) => { + return Promise.all([ + resp.ok, + resp.status, + resp.text(), + resp.headers, + ]); + }) + .then(([ok, status, data, headers]) => { + if (ok) { + return { + ok, + status, + data, + headers, + }; + } else { + throw new Error(JSON.stringify(json.error)); + } + }) + .then((resp) => { + let data = marked.parse(resp.data); + cfg.el.innerHTML = data; + }) + .catch((error) => { + console.error(error); + cfg.el.innerHTML = + '
'; + }); + }; +}; + +$(function () { + const els = document.getElementsByClassName('stellar-marked-api'); + for (var i = 0; i < els.length; i++) { + var cfg = new Object(); + const el = els[i]; + cfg.src = `${el.getAttribute('src')}?t=${new Date().getTime()}`; + cfg.el = el; + loadMarkdown(cfg); + } +});