Browse Source

web: add anchor to the title

Le Tan 7 years ago
parent
commit
7955246a51

+ 22 - 0
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 */

+ 14 - 1
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 = {
+            '&': '&',
+            '<': '&lt;',
+            '>': '&gt;',
+            '"': '&quot;',
+            "'": '&#039;'
+        };
+
+        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)
         });
     }
 

+ 1 - 0
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++;
     },

+ 10 - 9
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

+ 15 - 7
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 =
-          '<a name="' +
-          anchorName +
-          '" class="' +
-          options.anchorClass +
-          '" href="#"></a>';
+        if (options.addHeadingID) {
+          // No need to add id in anchor.
+          anchorToken.content = '<a class="' + options.anchorClass + '" ' +
+                                'href="#' + anchorName + '" ' +
+                                'data-anchor-icon="' + options.anchorIcon + '" ' +
+                                '></a>';
+        } else {
+          anchorToken.content = '<a id="' + anchorName + '" ' +
+                                'class="' + options.anchorClass + '" ' +
+                                'href="#' + anchorName + '" ' +
+                                'data-anchor-icon="' + options.anchorIcon + '" ' +
+                                '></a>';
+        }
 
-        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) {}
   };

+ 6 - 5
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;