wolegca.github.io/js/index.js

71 lines
2.0 KiB
JavaScript
Raw Normal View History

2019-09-20 22:34:39 +08:00
var index = getQueryString('i');
2019-09-20 12:31:47 +08:00
$.ajax({
type: "GET",
2019-09-20 22:34:39 +08:00
url: "content.json?timestamp=" + (new Date()).valueOf(),
2019-09-20 12:31:47 +08:00
dataType: "json",
success: function (response) {
var lastIndex = response.length - 1;
2019-09-20 22:34:39 +08:00
2019-10-19 21:56:07 +08:00
if (response.length <= index) {
2022-09-24 22:47:02 +08:00
message.alert('出错了!', '网络连接出错了,请稍后再试');
2019-09-21 16:07:59 +08:00
return;
}
2019-09-20 22:34:39 +08:00
listContent(response, lastIndex);
2019-09-20 12:31:47 +08:00
2022-09-24 22:47:02 +08:00
if (response[lastIndex]['type'] == 'html') {
2022-09-24 22:32:41 +08:00
$.ajax({
type: "GET",
url: 'content/html/' + response[lastIndex]['date'] + '.html',
success: function (res) {
appendContent(res);
},
error: function () {
message.alert('出错了!', '网络连接出错了,请稍后再试');
}
});
2022-09-24 22:47:02 +08:00
} else {
$.ajax({
type: "GET",
url: 'content/md/' + response[lastIndex]['date'] + '.md',
success: function (res) {
let md = window.markdownit({
html: true,
breaks: true,
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
const preCode = hljs.highlight(str, { language: lang }).value;
// 以换行进行分割
const lines = preCode.split(/\n/).slice(0, -1)
// 添加自定义行号
let html = lines.map((item, index) => {
return '<li><span class="line-num"></span>' + item + '</li>'
}).join('')
html = '<ol>' + html + '</ol>'
// 添加代码语言
if (lines.length) {
html += '<b class="name">' + lang + '</b>'
}
return '<pre><code>' +
html +
'</code></pre>'
} catch (e) { }
}
return ''; // use external default escaping
}
});
appendContent(md.render(res));
2022-09-24 22:56:57 +08:00
loadCss("css/highlight.css");
2022-09-24 22:52:35 +08:00
loadCss('js/highlight/styles/atom-one-light.min.css')
2022-09-24 22:47:02 +08:00
},
error: function () {
message.alert('出错了!', '网络连接出错了,请稍后再试');
}
});
2022-09-24 22:32:41 +08:00
}
2019-09-21 16:17:12 +08:00
},
2019-10-19 21:56:07 +08:00
error: function () {
message.alert('出错了!', '网络连接出错了,请稍后再试');
2019-09-20 12:31:47 +08:00
}
});