From 7955246a5124f2335198c45f10f83b73bdd88333 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Tue, 20 Nov 2018 20:13:49 +0800 Subject: [PATCH] web: add anchor to the title --- src/resources/common.css | 22 +++++++++++++++++++ src/resources/export/outline.js | 15 ++++++++++++- src/resources/markdown-it.js | 1 + src/utils/markdown-it/README.md | 19 ++++++++-------- .../markdown-it/markdown-it-headinganchor.js | 22 +++++++++++++------ src/vfile.cpp | 11 +++++----- 6 files changed, 68 insertions(+), 22 deletions(-) diff --git a/src/resources/common.css b/src/resources/common.css index f3f8b70b..9551995b 100644 --- a/src/resources/common.css +++ b/src/resources/common.css @@ -186,3 +186,25 @@ span.modal-close:focus { border-color: #c6c8ca; } /* Alert */ + +/* Anchor */ +.vnote-anchor { + font-weight: 400; + color: rgba(0,123,255,.5); + transition: color .16s linear; + padding-left: 0.375em; + -webkit-font-smoothing: antialiased; + text-decoration: none; + opacity: 0; +} + +.vnote-anchor:hover { + color: rgba(0,123,255,1); + text-decoration: none; + opacity: 1; +} + +.vnote-anchor::after { + content: attr(data-anchor-icon); +} +/* Anchor */ diff --git a/src/resources/export/outline.js b/src/resources/export/outline.js index 66c5909c..748f9f85 100644 --- a/src/resources/export/outline.js +++ b/src/resources/export/outline.js @@ -46,6 +46,19 @@ window.addEventListener('load', function() { var outlineContent = document.getElementById('outline-content'); var postContent = document.getElementById('post-content'); + // Escape @text to Html. + var escapeHtml = function(text) { + var map = { + '&': '&', + '<': '<', + '>': '>', + '"': '"', + "'": ''' + }; + + return text.replace(/[&<>"']/g, function(m) { return map[m]; }); + } + // Fetch the outline. var headers = postContent.querySelectorAll("h1, h2, h3, h4, h5, h6"); toc = []; @@ -55,7 +68,7 @@ window.addEventListener('load', function() { toc.push({ level: parseInt(header.tagName.substr(1)), anchor: header.id, - title: header.textContent + title: escapeHtml(header.textContent) }); } diff --git a/src/resources/markdown-it.js b/src/resources/markdown-it.js index f6b298f7..1e3b4710 100644 --- a/src/resources/markdown-it.js +++ b/src/resources/markdown-it.js @@ -60,6 +60,7 @@ mdit = mdit.use(window.markdownitHeadingAnchor, { anchorClass: 'vnote-anchor', addHeadingID: true, addHeadingAnchor: true, + anchorIcon: '#', slugify: function(md, s) { return 'toc_' + nameCounter++; }, diff --git a/src/utils/markdown-it/README.md b/src/utils/markdown-it/README.md index c888d71d..4ce9b7e5 100644 --- a/src/utils/markdown-it/README.md +++ b/src/utils/markdown-it/README.md @@ -13,33 +13,34 @@ v1.4.0 Revin Guillen # [markdown-it-footnote](https://github.com/markdown-it/markdown-it-footnote) -v3.0.1 +v3.0.1 Vitaly Puzrin # [markdown-it-sub](https://github.com/markdown-it/markdown-it-sub) -v1.0.0 +v1.0.0 Vitaly Puzrin # [markdown-it-sup](https://github.com/markdown-it/markdown-it-sup) -v1.0.0 +v1.0.0 Vitaly Puzrin # [markddown-it-front-matter](https://github.com/craigdmckenna/markdown-it-front-matter) -v0.1.2 +v0.1.2 Craig McKenna # [markdown-it-imsize](https://github.com/tatsy/markdown-it-imsize) -v2.0.1 +v2.0.1 Tatsuya Yatagawa # [markdown-it-emoji](https://github.com/markdown-it/markdown-it-emoji) -v1.4.0 +v1.4.0 Vitaly Puzrin # [markdown-it-texmath](https://github.com/goessner/markdown-it-texmath) -v0.0.0 +v0.0.0 Stefan Goessner +Modified by Le Tan # [markdown-it-container](https://github.com/markdown-it/markdown-it-container) -v2.0.0 -Vitaly Puzrin +v2.0.0 +Vitaly Puzrin \ No newline at end of file diff --git a/src/utils/markdown-it/markdown-it-headinganchor.js b/src/utils/markdown-it/markdown-it-headinganchor.js index ca714c01..93c872d6 100644 --- a/src/utils/markdown-it/markdown-it-headinganchor.js +++ b/src/utils/markdown-it/markdown-it-headinganchor.js @@ -39,14 +39,21 @@ function makeRule(md, options) { if (options.addHeadingAnchor) { var anchorToken = new state.Token('html_inline', '', 0); - anchorToken.content = - ''; + if (options.addHeadingID) { + // No need to add id in anchor. + anchorToken.content = ''; + } else { + anchorToken.content = ''; + } - headingInlineToken.children.unshift(anchorToken); + headingInlineToken.children.push(anchorToken); } // Advance past the inline and heading_close tokens. @@ -61,6 +68,7 @@ module.exports = function headinganchor_plugin(md, opts) { addHeadingID: true, addHeadingAnchor: true, // Added by Le Tan (github.com/tamlok) + anchorIcon: '#', slugify: slugify, headingHook: function(openToken, inlineToken, anchor) {} }; diff --git a/src/vfile.cpp b/src/vfile.cpp index d33d0642..da896309 100644 --- a/src/vfile.cpp +++ b/src/vfile.cpp @@ -88,19 +88,20 @@ QUrl VFile::getBaseUrl() const { // Need to judge the path: Url, local file, resource file. QUrl baseUrl; - QString basePath = fetchBasePath(); - QFileInfo pathInfo(basePath); + // Use file path to make in page anchor work. + QString filePath = fetchPath(); + QFileInfo pathInfo(filePath); if (pathInfo.exists()) { if (pathInfo.isNativePath()) { // Local file. - baseUrl = QUrl::fromLocalFile(basePath + QDir::separator()); + baseUrl = QUrl::fromLocalFile(filePath); } else { // Resource file. - baseUrl = QUrl("qrc" + basePath + QDir::separator()); + baseUrl = QUrl("qrc" + filePath); } } else { // Url. - baseUrl = QUrl(basePath + QDir::separator()); + baseUrl = QUrl(filePath); } return baseUrl;