From 0433c9c1e101b74e6c64f6023dccd52a432ed3d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20Bo=C3=ABs?= Date: Wed, 22 Aug 2018 18:30:45 +0200 Subject: [PATCH] Handle internal links to `TITLE.md` and `../TITLE/index.md` and convert these into proper URLs --- lib/models/post.js | 2 +- test/post.js | 35 ++++++++++++++++------------------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/lib/models/post.js b/lib/models/post.js index 9992be8c..71651180 100644 --- a/lib/models/post.js +++ b/lib/models/post.js @@ -262,7 +262,7 @@ const Post = function(filename, markdown, meta, config) { } return imageStyles(config) .replaceImgHtml(html) - .replace(/(href=")([a-zA-Z0-9-]+)\.md(")/g, '$1' + config.basePath + config.htdocs.posts + '/$2/$3') + .replace(/(href=")(?:\.\.\/)?([^/]+)(?:\/index)?\.md(")/g, '$1' + config.basePath + config.htdocs.posts + '/$2/$3') ; }; diff --git a/test/post.js b/test/post.js index 8f425b97..8f735dc2 100644 --- a/test/post.js +++ b/test/post.js @@ -144,12 +144,12 @@ describe('Post', function() { }); it('should test Links', function() { - const markdown = 'www.1test.com _will_ be found as of `marked@0.3.15` ' - + 'And [this link](http://www.2test.com) will be found.' - + 'As will be [this link](https://www.3test.com).' - + 'An ![image](https://www.4test.com) should not be found.' - + 'As will be [this link](https://www.5test.com/some-compilcated-foo?a#b).' - + 'But not example.6test.com or 7test.com (URLs with subdomains without proper links)' + const markdown = `www.1test.com _will_ be found as of \`marked@0.3.15\` +And [this link](http://www.2test.com) will be found. +As will be [this link](https://www.3test.com). +An ![image](https://www.4test.com) should not be found. +As will be [this link](https://www.5test.com/some-compilcated-foo?a#b). +But not example.6test.com or 7test.com (URLs with subdomains without proper links)` ; const html = post('test.md', markdown, { Description: 'None', @@ -166,22 +166,19 @@ describe('Post', function() { assert.equal(testLinks[3], 'https://www.5test.com/some-compilcated-foo?a#b'); }); - -/* - it('should test SpecialProperties', function() { - var testPost = post('test.md', 'Test', { + it('should convert internal links', function() { + const markdown = `This is an [internal link](internal-link.md), as is [this](internal-link2/index.md) and [this](../internal-link3/index.md).`; + const testPost = post('test.md', markdown, { Description: 'Description', Date: new Date() }); + const testLinks = testPost.getAllExternalLinks(); + //console.log(testPost.html); - if (config.specialFeatures.acceleratedmobilepages) { - assert.ok(testPost.safeHtmlTeaser); - assert.ok(testPost.safeHtmlTeaser === '

Description

'); - } - if (config.specialFeatures.jsonrss || config.specialFeatures.atom || config.specialFeatures.rss) { - assert.ok(testPost.ampHtml); - assert.ok(testPost.ampHtmlTeaser === '

Description

'); - } + assert.equal(testLinks.length, 0); + assert.ok(testPost.html.match(/\/posts\/internal-link\//)); + assert.ok(testPost.html.match(/\/posts\/internal-link2\//)); + assert.ok(testPost.html.match(/\/posts\/internal-link3\//)); }); -*/ + });