From 8d889a6736e8dc5a7ad7252b7fa8ee49b5764fa3 Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Sat, 3 Aug 2019 11:53:42 +0800 Subject: [PATCH 1/2] Support "spoiler" container syntax Signed-off-by: Yukai Huang --- public/js/extra.js | 16 ++++++++++++++++ public/js/index.js | 2 +- public/js/lib/syncscroll.js | 16 ++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/public/js/extra.js b/public/js/extra.js index 27492254..658ad751 100644 --- a/public/js/extra.js +++ b/public/js/extra.js @@ -993,6 +993,22 @@ md.use(markdownitContainer, 'success', { render: renderContainer }) md.use(markdownitContainer, 'info', { render: renderContainer }) md.use(markdownitContainer, 'warning', { render: renderContainer }) md.use(markdownitContainer, 'danger', { render: renderContainer }) +md.use(markdownitContainer, 'spoiler', { + validate: function (params) { + return params.trim().match(/^spoiler\s+(.*)$/) + }, + render: function (tokens, idx) { + var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/) + + if (tokens[idx].nesting === 1) { + // opening tag + return '
' + md.utils.escapeHtml(m[1]) + '\n' + } else { + // closing tag + return '
\n' + } + } +}) let defaultImageRender = md.renderer.rules.image md.renderer.rules.image = function (tokens, idx, options, env, self) { diff --git a/public/js/index.js b/public/js/index.js index 2d93d173..7e774982 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -95,7 +95,7 @@ var updateViewDebounce = 100 var cursorMenuThrottle = 50 var cursorActivityDebounce = 50 var cursorAnimatePeriod = 100 -var supportContainers = ['success', 'info', 'warning', 'danger'] +var supportContainers = ['success', 'info', 'warning', 'danger', 'spoiler'] var supportCodeModes = ['javascript', 'typescript', 'jsx', 'htmlmixed', 'htmlembedded', 'css', 'xml', 'clike', 'clojure', 'ruby', 'python', 'shell', 'php', 'sql', 'haskell', 'coffeescript', 'yaml', 'pug', 'lua', 'cmake', 'nginx', 'perl', 'sass', 'r', 'dockerfile', 'tiddlywiki', 'mediawiki', 'go', 'gherkin'].concat(hljs.listLanguages()) var supportCharts = ['sequence', 'flow', 'graphviz', 'mermaid', 'abc'] var supportHeaders = [ diff --git a/public/js/lib/syncscroll.js b/public/js/lib/syncscroll.js index d492fbc9..ec20e777 100644 --- a/public/js/lib/syncscroll.js +++ b/public/js/lib/syncscroll.js @@ -110,6 +110,22 @@ md.use(markdownitContainer, 'success', { render: renderContainer }) md.use(markdownitContainer, 'info', { render: renderContainer }) md.use(markdownitContainer, 'warning', { render: renderContainer }) md.use(markdownitContainer, 'danger', { render: renderContainer }) +md.use(markdownitContainer, 'spoiler', { + validate: function (params) { + return params.trim().match(/^spoiler\s+(.*)$/) + }, + render: function (tokens, idx) { + var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/) + + if (tokens[idx].nesting === 1) { + // opening tag + return '
' + md.utils.escapeHtml(m[1]) + '\n' + } else { + // closing tag + return '
\n' + } + } +}) window.preventSyncScrollToEdit = false window.preventSyncScrollToView = false From 6f7de250792a26b2df7fc9993d936d81b8cf3571 Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Sat, 3 Aug 2019 15:11:37 +0800 Subject: [PATCH 2/2] Make details element syncscrollable Signed-off-by: Yukai Huang --- public/js/extra.js | 6 ++++++ public/js/lib/syncscroll.js | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/public/js/extra.js b/public/js/extra.js index 658ad751..2024c297 100644 --- a/public/js/extra.js +++ b/public/js/extra.js @@ -539,6 +539,12 @@ export function finishView (view) { } catch (err) { console.warn(err) } + + // register details toggle for scrollmap recalulation + view.find('details.raw').removeClass('raw').each(function (key, val) { + $(val).on('toggle', window.viewAjaxCallback) + }) + // render title document.title = renderTitle(view) } diff --git a/public/js/lib/syncscroll.js b/public/js/lib/syncscroll.js index ec20e777..738ff1ce 100644 --- a/public/js/lib/syncscroll.js +++ b/public/js/lib/syncscroll.js @@ -118,8 +118,11 @@ md.use(markdownitContainer, 'spoiler', { var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/) if (tokens[idx].nesting === 1) { + const startline = tokens[idx].map[0] + 1 + const endline = tokens[idx].map[1] + // opening tag - return '
' + md.utils.escapeHtml(m[1]) + '\n' + return `
` + md.utils.escapeHtml(m[1]) + '\n' } else { // closing tag return '
\n'