Skip to content

Commit

Permalink
Add linkification for local links that point to .md files
Browse files Browse the repository at this point in the history
  • Loading branch information
mixu committed Oct 30, 2015
1 parent 4d56312 commit eaca75c
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 3 deletions.
42 changes: 41 additions & 1 deletion 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
Expand Down Expand Up @@ -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 = '<a href="' + href + '"';
if (title) {
out += ' title="' + title + '"';
}
out += '>' + text + '</a>';
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() {
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Expand Up @@ -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)` -> `<a href="./foo.html">link</a>`.
- `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.
Expand Down
32 changes: 32 additions & 0 deletions test/integration.test.js
Expand Up @@ -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, [
'<h1 id="test"><a class="header-link" href="#test"></a>Test</h1>',
'<p><a href="./foo.html">foo</a>',
'<a href="./bar.html">bar</a>',
'<a href="/baz.html">baz</a>',
'<a href="./multi.md.html">multi</a>',
'<a href="http://foo.md">http</a>',
'<a href="https://foo.md">https</a>',
'<a href="ftp://foo.md">ftp</a>',
'<a href="javascript://foo.md">javascript</a></p>\n',
].join('\n'));
done();
});
});
});
});
33 changes: 32 additions & 1 deletion todo.md
Expand Up @@ -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
Expand Down

0 comments on commit eaca75c

Please sign in to comment.