From 096be72b3f805ef5f6b92310f527207a499af332 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Santos?= Date: Sat, 12 Sep 2015 15:27:21 +0100 Subject: [PATCH 1/2] implement anchor links --- build.js | 17 ++++++++++++++++- layouts/css/base.styl | 1 + layouts/css/page-modules/_anchorLinks.styl | 3 +++ package.json | 1 + 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 layouts/css/page-modules/_anchorLinks.styl diff --git a/build.js b/build.js index ff9b3d108e50d..ad1ffcd987876 100755 --- a/build.js +++ b/build.js @@ -11,6 +11,7 @@ const markdown = require('metalsmith-markdown'); const prism = require('metalsmith-prism'); const stylus = require('metalsmith-stylus'); const permalinks = require('metalsmith-permalinks'); +const marked = require('marked'); const path = require('path'); const fs = require('fs'); const ncp = require('ncp'); @@ -25,6 +26,20 @@ const versions = require('./source/versions'); // for properties which are not present in the given language const DEFAULT_LANG = 'en'; + +// Add an anchor link. Taken directly from the marked docs +const renderer = new marked.Renderer(); +renderer.heading = function (text, level) { + var escapedText = text.toLowerCase().replace(/[^\w]+/g, '-'); + + return '' + text + '' + + ''; +}; +const markedOptions = { langPrefix: 'language-', renderer: renderer }; + function i18nJSON (lang) { var defaultJSON = require(`./locale/${DEFAULT_LANG}/site.json`); var templateJSON = require(`./locale/${lang}/site.json`); @@ -104,7 +119,7 @@ function buildlocale (locale) { refer: false } })) - .use(markdown({ langPrefix: 'language-' })) + .use(markdown(markedOptions)) .use(prism()) .use(filterStylusPartials()) .use(stylus({ diff --git a/layouts/css/base.styl b/layouts/css/base.styl index d16c1d2c47d83..fe8cad6ceafc0 100644 --- a/layouts/css/base.styl +++ b/layouts/css/base.styl @@ -78,6 +78,7 @@ pre @import 'page-modules/_in-the-news' @import 'page-modules/_download' @import 'page-modules/_scrollToTop' +@import 'page-modules/_anchorLinks' .intro margin-top 140px diff --git a/layouts/css/page-modules/_anchorLinks.styl b/layouts/css/page-modules/_anchorLinks.styl new file mode 100644 index 0000000000000..cbaac42c1d97a --- /dev/null +++ b/layouts/css/page-modules/_anchorLinks.styl @@ -0,0 +1,3 @@ +// Anchor links to parts of the page +.anchor:before + content '#' diff --git a/package.json b/package.json index 116ca2bedaa43..27e2a1cd91f18 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "chokidar": "^1.0.5", "handlebars": "^3.0.0", "map-async": "~0.1.1", + "marked": "^0.3.5", "metalsmith": "^2.0.0", "metalsmith-collections": "^0.7.0", "metalsmith-feed": "0.0.6", From 23df641b1e134e21c93d84daf7fc05266c851cb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Santos?= Date: Sat, 12 Sep 2015 15:31:59 +0100 Subject: [PATCH 2/2] make them show up only on hover --- layouts/css/page-modules/_anchorLinks.styl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/layouts/css/page-modules/_anchorLinks.styl b/layouts/css/page-modules/_anchorLinks.styl index cbaac42c1d97a..04f98e5b94332 100644 --- a/layouts/css/page-modules/_anchorLinks.styl +++ b/layouts/css/page-modules/_anchorLinks.styl @@ -1,3 +1,8 @@ // Anchor links to parts of the page -.anchor:before +h1:hover > .anchor:before, +h2:hover > .anchor:before, +h3:hover > .anchor:before, +h4:hover > .anchor:before, +h5:hover > .anchor:before, +h6:hover > .anchor:before content '#'