From eaca75c5cb7dd2878c9fdb88585e3949380a0b2b Mon Sep 17 00:00:00 2001 From: Mikito Takada Date: Thu, 29 Oct 2015 20:25:00 -0700 Subject: [PATCH] Add linkification for local links that point to .md files --- lib/convert-md.js | 42 +++++++++++++++++++++++++++++++++++++++- readme.md | 4 +++- test/integration.test.js | 32 ++++++++++++++++++++++++++++++ todo.md | 33 ++++++++++++++++++++++++++++++- 4 files changed, 108 insertions(+), 3 deletions(-) diff --git a/lib/convert-md.js b/lib/convert-md.js index 4177b26..9e9182c 100644 --- a/lib/convert-md.js +++ b/lib/convert-md.js @@ -1,4 +1,6 @@ -var pi = require('pipe-iterators'), +var url = require('url'), + path = require('path'), + pi = require('pipe-iterators'), md = require('markdown-stream-utils'); // A wrapper around md.convertMd that includes the custom heading generation and id @@ -37,6 +39,44 @@ module.exports = function(argv) { '>\n'; }; + renderer.link = function(href, title, text) { + if (this.options.sanitize) { + try { + var prot = decodeURIComponent(unescape(href)) + .replace(/[^\w:]/g, '') + .toLowerCase(); + } catch (e) { + return ''; + } + if (prot.indexOf('javascript:') === 0 || prot.indexOf('vbscript:') === 0) { + return ''; + } + } + var parsed = url.parse(href); + // convert [.md] in local links (e.g. links with no protocol) + if (!parsed.protocol) { + var ext = path.extname(parsed.pathname); + if (ext === '.markdown' || + ext === '.mdown' || + ext === '.mkd' || + ext === '.mkdn' || + ext === '.md') { + var dirname = path.dirname(parsed.pathname); + parsed.pathname = dirname + + (dirname.charAt(dirname.length - 1) === '/' ? '' : '/') + + path.basename(parsed.pathname, ext) + '.html'; + href = url.format(parsed); + } + } + + var out = ''; + return out; + }; + // reset the header counts for each file, so that idCount is not shared across the whole render return pi.pipeline([ pi.forEach(function() { diff --git a/readme.md b/readme.md index 41c5740..e27d1f9 100644 --- a/readme.md +++ b/readme.md @@ -6,7 +6,9 @@ Looking for something to generate a blog from Markdown files? Check out [ghost-r ## Features -- `v3.1.2` adds default classes that allow you to [style headings in the table of contents](#table-of-contents). See [the changelog](./changelog.md) for changes made in older versions. +- `v3.1.4` adds linkification for relative links to markdown files, e.g. `[link](./foo.md)` -> `link`. +- `v3.1.3` added a few additional properties to the programmatic API. +- `v3.1.2` added default classes that allow you to [style headings in the table of contents](#table-of-contents). See [the changelog](./changelog.md) for changes made in older versions. - Includes 15+ ready-made CSS stylesheets for Markdown, see the bottom of the readme for screenshots. - Reuse the stylesheets or use the `generate-md` tool to convert a folder of Markdown files to HTML using one of the built-in layouts or a custom layout. - Completely static output is easy to host anywhere. diff --git a/test/integration.test.js b/test/integration.test.js index 4513b11..c375174 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -312,5 +312,37 @@ describe('integration tests', function() { done(); }); }); + + it('replaces .md in local links but not in remote links ', function(done) { + render({ + contents: [ + 'title: Hello world', + 'author: Anonymous', + '----', + '# Test', + '[foo](./foo.md)', + '[bar](bar.md)', + '[baz](/baz.md)', + '[multi](./multi.md.md)', + '[http](http://foo.md)', + '[https](https://foo.md)', + '[ftp](ftp://foo.md)', + '[javascript](javascript://foo.md)', + ].join('\n') + }, { template: '{{> content}}' }, function(html) { + assert.equal(html, [ + '

Test

', + '

foo', + 'bar', + 'baz', + 'multi', + 'http', + 'https', + 'ftp', + 'javascript

\n', + ].join('\n')); + done(); + }); + }); }); }); diff --git a/todo.md b/todo.md index 96f22f3..5ae12c9 100644 --- a/todo.md +++ b/todo.md @@ -9,9 +9,40 @@ - ship integration: https://github.com/carrot/ship - emoji support - static asset revisioning via https://www.npmjs.com/package/rev-hash, https://github.com/galkinrost/gulp-rev-css-url +- better theming: + - grid system: bootstrap (grid only) + - typography: + - heading font + - body font + - code font + - color palette + - material design shades, e.g. https://www.materialpalette.com/pink/indigo + - background color + - text color + - heading text + - body text + - link color + - emphasis + - styles for bold text + - styles for italic text + - components + - CSS + - headings + - paragraphs + - blockquotes + - ordered lists + - unordered lists + - nested lists + - inline code blocks + - code blocks + - images + - tables + - horizontal rule + - nav bar + - menu + - headings (hover link) - express plugin e.g. equivalent to express.static() - docco style -- linkify .md links - themes - https://github.com/nrbernard/droplr-markdown-css - https://github.com/chrisopedia/base16-marked