From 418682e4a3b203aa98f14114a0b8132904589b24 Mon Sep 17 00:00:00 2001 From: Ilya Radchenko Date: Tue, 5 Nov 2019 10:53:32 -0500 Subject: [PATCH 01/18] fix: convert os specific paths in toc --- lib/tocify.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/tocify.js b/lib/tocify.js index 479b55b..173b083 100644 --- a/lib/tocify.js +++ b/lib/tocify.js @@ -137,6 +137,9 @@ class Tocify { // `source`: Takes care of relative (../) paths assign(item, absolutify(item.source, this.docs)) + // `source`: Handle OS specific paths + assign(item, pathify(item.source)) + if (this.sources[item.source]) { // If this same item exists before, reuse its URL and stuff const previous = this.sources[item.source] @@ -227,3 +230,19 @@ function absolutify (source, root) { source = source.replace(/^\//, '') return { source } } + +/* + * Internal: takes care of OS specific paths. + * + * pathify("..\README.md", "docs") => "../README.md" + * pathify("\README.md", "docs") => "/README.md" + */ + +function pathify (source) { + if (externalSource(source)) { + return { source } + } + + source = source.replace(/\\/g, '/') + return { source } +} From 0b68cd0d18b3e1e476efc1879d3c6314439b7fc6 Mon Sep 17 00:00:00 2001 From: Ilya Radchenko Date: Tue, 5 Nov 2019 10:54:13 -0500 Subject: [PATCH 02/18] chore: use 'node' instead of stable, which doesn't seem to work on windows --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 1a9705a..e372404 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,7 @@ os: - windows - linux node_js: - - 'stable' + - 'node' - '10' - '8' cache: From f7b0c41987cb1e46feadee2b91ea2e0e266b3db8 Mon Sep 17 00:00:00 2001 From: Ilya Radchenko Date: Tue, 5 Nov 2019 11:07:05 -0500 Subject: [PATCH 03/18] chore: log target --- lib/fix_html.js | 2 +- test/index/invalid_refs_test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/fix_html.js b/lib/fix_html.js index 89ba555..ac58af0 100644 --- a/lib/fix_html.js +++ b/lib/fix_html.js @@ -61,7 +61,7 @@ function fixReferences ($, fname, sources, files, page) { // Ensure that it's available. if (!sources[target]) { - throw new Error(`${base}: Unknown reference '${origUrl}'`) + throw new Error(`${base}: Unknown reference '${origUrl}' (target: ${target})`) } // Relativize that absolute URL. diff --git a/test/index/invalid_refs_test.js b/test/index/invalid_refs_test.js index 0ecc310..216e80f 100644 --- a/test/index/invalid_refs_test.js +++ b/test/index/invalid_refs_test.js @@ -26,6 +26,6 @@ describe('index/invalid refs:', function () { it('catches invalid references', function () { expect(this.err).toBeDefined() expect(this.err.message).toEqual( - "README.md: Unknown reference 'docs/getting-started.md'") + "README.md: Unknown reference 'docs/getting-started.md' (target: docs/getting-started.md)") }) }) From 97430f4fb4a9dc17b86eca7156dc677db6395c42 Mon Sep 17 00:00:00 2001 From: Ilya Radchenko Date: Tue, 5 Nov 2019 11:16:33 -0500 Subject: [PATCH 04/18] fix: convert to / when getting target --- lib/fix_html.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/fix_html.js b/lib/fix_html.js index ac58af0..c10f092 100644 --- a/lib/fix_html.js +++ b/lib/fix_html.js @@ -78,6 +78,7 @@ function fixReferences ($, fname, sources, files, page) { */ function getTarget (url, base) { + url = url.replace(/\\/g, '/') if (url.substr(0, 1) === '/') { return url.substr(1) } else { From eea7229c1b8c39c679d160350dcae1de5fdfcb73 Mon Sep 17 00:00:00 2001 From: Ilya Radchenko Date: Tue, 5 Nov 2019 11:17:54 -0500 Subject: [PATCH 05/18] fix: convert / on base too --- lib/fix_html.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/fix_html.js b/lib/fix_html.js index c10f092..03573cc 100644 --- a/lib/fix_html.js +++ b/lib/fix_html.js @@ -79,6 +79,10 @@ function fixReferences ($, fname, sources, files, page) { function getTarget (url, base) { url = url.replace(/\\/g, '/') + if (base) { + base = base.replace(/\\/g, '/') + } + if (url.substr(0, 1) === '/') { return url.substr(1) } else { From dbb35bcdf2c67e2a81135596b5906b404cb0741d Mon Sep 17 00:00:00 2001 From: Ilya Radchenko Date: Tue, 5 Nov 2019 11:31:01 -0500 Subject: [PATCH 06/18] chore: lint --- lib/fix_html.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fix_html.js b/lib/fix_html.js index 03573cc..e7ea55f 100644 --- a/lib/fix_html.js +++ b/lib/fix_html.js @@ -82,7 +82,7 @@ function getTarget (url, base) { if (base) { base = base.replace(/\\/g, '/') } - + if (url.substr(0, 1) === '/') { return url.substr(1) } else { From 9e710c1ce65cc4ffe9cb628bea6fe07388d9ff6c Mon Sep 17 00:00:00 2001 From: Ilya Radchenko Date: Tue, 5 Nov 2019 11:54:57 -0500 Subject: [PATCH 07/18] fix: handle \\ instead --- lib/fix_html.js | 4 ++-- lib/tocify.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/fix_html.js b/lib/fix_html.js index e7ea55f..a3de1f7 100644 --- a/lib/fix_html.js +++ b/lib/fix_html.js @@ -78,9 +78,9 @@ function fixReferences ($, fname, sources, files, page) { */ function getTarget (url, base) { - url = url.replace(/\\/g, '/') + url = url.replace(/\\\\/g, '/') if (base) { - base = base.replace(/\\/g, '/') + base = base.replace(/\\\\/g, '/') } if (url.substr(0, 1) === '/') { diff --git a/lib/tocify.js b/lib/tocify.js index 173b083..57af881 100644 --- a/lib/tocify.js +++ b/lib/tocify.js @@ -243,6 +243,6 @@ function pathify (source) { return { source } } - source = source.replace(/\\/g, '/') + source = source.replace(/\\\\/g, '/') return { source } } From 34d324312103134208f7017d363dde19ab113d1f Mon Sep 17 00:00:00 2001 From: Ilya Radchenko Date: Tue, 5 Nov 2019 11:58:35 -0500 Subject: [PATCH 08/18] handle both \\ and \ --- lib/fix_html.js | 2 ++ lib/tocify.js | 1 + 2 files changed, 3 insertions(+) diff --git a/lib/fix_html.js b/lib/fix_html.js index a3de1f7..a5a0045 100644 --- a/lib/fix_html.js +++ b/lib/fix_html.js @@ -79,8 +79,10 @@ function fixReferences ($, fname, sources, files, page) { function getTarget (url, base) { url = url.replace(/\\\\/g, '/') + .replace(/\\/g, '/') if (base) { base = base.replace(/\\\\/g, '/') + .replace(/\\/g, '/') } if (url.substr(0, 1) === '/') { diff --git a/lib/tocify.js b/lib/tocify.js index 57af881..df6c5d6 100644 --- a/lib/tocify.js +++ b/lib/tocify.js @@ -244,5 +244,6 @@ function pathify (source) { } source = source.replace(/\\\\/g, '/') + .replace(/\\/g, '/') return { source } } From cd14e7912973c2550e02d268cc603c6c9d400db4 Mon Sep 17 00:00:00 2001 From: Ilya Radchenko Date: Tue, 5 Nov 2019 12:03:29 -0500 Subject: [PATCH 09/18] fix: move to helper --- lib/fix_html.js | 14 ++++---------- lib/helpers/normalize_path.js | 9 +++++++++ lib/tocify.js | 4 ++-- 3 files changed, 15 insertions(+), 12 deletions(-) create mode 100644 lib/helpers/normalize_path.js diff --git a/lib/fix_html.js b/lib/fix_html.js index a5a0045..e465c3a 100644 --- a/lib/fix_html.js +++ b/lib/fix_html.js @@ -5,6 +5,7 @@ const slugify = require('slugify') const resolve = require('path').resolve const dirname = require('path').dirname const relative = require('path').relative +const normalizePath = require('./helpers/normalize_path'); /** * Internal: Performs in-place HTML filters (such as fixing URLs). @@ -33,7 +34,7 @@ function idify ($) { */ function fixReferences ($, fname, sources, files, page) { - var base = page.source + var base = normalizePath(page.source) var m $('[href], [src]').each(function () { @@ -57,7 +58,7 @@ function fixReferences ($, fname, sources, files, page) { } // Get the target source file it points to (eg, `docs/usage.md`). - var target = getTarget(origUrl, base) + var target = normalizePath(getTarget(origUrl, base)) // Ensure that it's available. if (!sources[target]) { @@ -78,16 +79,9 @@ function fixReferences ($, fname, sources, files, page) { */ function getTarget (url, base) { - url = url.replace(/\\\\/g, '/') - .replace(/\\/g, '/') - if (base) { - base = base.replace(/\\\\/g, '/') - .replace(/\\/g, '/') - } - if (url.substr(0, 1) === '/') { return url.substr(1) } else { return resolve('/' + dirname(base) + '/' + url).substr(1) } -} +} \ No newline at end of file diff --git a/lib/helpers/normalize_path.js b/lib/helpers/normalize_path.js new file mode 100644 index 0000000..a5434cf --- /dev/null +++ b/lib/helpers/normalize_path.js @@ -0,0 +1,9 @@ +module.exports = function fixSlashes (url) { + if (!url) { + return url + } + url = url.replace(/\\\\/g, '/') + .replace(/\\/g, '/') + + return url; +} \ No newline at end of file diff --git a/lib/tocify.js b/lib/tocify.js index df6c5d6..d125921 100644 --- a/lib/tocify.js +++ b/lib/tocify.js @@ -4,6 +4,7 @@ const marked = require('marked') const normalize = require('path').normalize const stripMarkdown = require('./helpers/strip_markdown') +const normalizePath = require('./helpers/normalize_path'); const assign = Object.assign const slugify = require('./slugify') @@ -243,7 +244,6 @@ function pathify (source) { return { source } } - source = source.replace(/\\\\/g, '/') - .replace(/\\/g, '/') + source = normalizePath(source) return { source } } From d7910af39219cdab0451c70710fbac0650a7d4d3 Mon Sep 17 00:00:00 2001 From: Ilya Radchenko Date: Tue, 5 Nov 2019 12:15:24 -0500 Subject: [PATCH 10/18] lint --- lib/fix_html.js | 4 ++-- lib/helpers/normalize_path.js | 4 ++-- lib/tocify.js | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/fix_html.js b/lib/fix_html.js index e465c3a..e90de6c 100644 --- a/lib/fix_html.js +++ b/lib/fix_html.js @@ -5,7 +5,7 @@ const slugify = require('slugify') const resolve = require('path').resolve const dirname = require('path').dirname const relative = require('path').relative -const normalizePath = require('./helpers/normalize_path'); +const normalizePath = require('./helpers/normalize_path') /** * Internal: Performs in-place HTML filters (such as fixing URLs). @@ -84,4 +84,4 @@ function getTarget (url, base) { } else { return resolve('/' + dirname(base) + '/' + url).substr(1) } -} \ No newline at end of file +} diff --git a/lib/helpers/normalize_path.js b/lib/helpers/normalize_path.js index a5434cf..bc7d2f2 100644 --- a/lib/helpers/normalize_path.js +++ b/lib/helpers/normalize_path.js @@ -5,5 +5,5 @@ module.exports = function fixSlashes (url) { url = url.replace(/\\\\/g, '/') .replace(/\\/g, '/') - return url; -} \ No newline at end of file + return url +} diff --git a/lib/tocify.js b/lib/tocify.js index d125921..7d8ce1f 100644 --- a/lib/tocify.js +++ b/lib/tocify.js @@ -4,7 +4,7 @@ const marked = require('marked') const normalize = require('path').normalize const stripMarkdown = require('./helpers/strip_markdown') -const normalizePath = require('./helpers/normalize_path'); +const normalizePath = require('./helpers/normalize_path') const assign = Object.assign const slugify = require('./slugify') From d189b9fbe6b5d92c5f1ffd22b20d4e232d3c0bcf Mon Sep 17 00:00:00 2001 From: Ilya Radchenko Date: Tue, 5 Nov 2019 12:45:13 -0500 Subject: [PATCH 11/18] more logging --- lib/tocify.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/tocify.js b/lib/tocify.js index 7d8ce1f..e369b7e 100644 --- a/lib/tocify.js +++ b/lib/tocify.js @@ -7,6 +7,7 @@ const stripMarkdown = require('./helpers/strip_markdown') const normalizePath = require('./helpers/normalize_path') const assign = Object.assign +const log = require('./log') const slugify = require('./slugify') const tocifyPage = require('./tocify_page') const externalSource = require('./external_source') @@ -92,6 +93,8 @@ class Tocify { } }) + log(`tocify(run): sources ${JSON.stringify(this.sources)}`) + return re } @@ -162,6 +165,8 @@ class Tocify { if (headings) item.headings = headings } + log(`tocify(itemify): item ${JSON.stringify(item)}`) + return item } From 7931ef9cf9c6f886e772b1c2f64e9a74ae5ac119 Mon Sep 17 00:00:00 2001 From: Ilya Radchenko Date: Tue, 5 Nov 2019 13:01:31 -0500 Subject: [PATCH 12/18] log sources --- lib/fix_html.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/fix_html.js b/lib/fix_html.js index e90de6c..ad7d10a 100644 --- a/lib/fix_html.js +++ b/lib/fix_html.js @@ -62,6 +62,7 @@ function fixReferences ($, fname, sources, files, page) { // Ensure that it's available. if (!sources[target]) { + console.log(sources) throw new Error(`${base}: Unknown reference '${origUrl}' (target: ${target})`) } From 746402b1a5edac8e2687959de53f185fbd6c2181 Mon Sep 17 00:00:00 2001 From: Ilya Radchenko Date: Tue, 5 Nov 2019 13:08:10 -0500 Subject: [PATCH 13/18] log again --- lib/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/index.js b/lib/index.js index 4befef4..929caad 100644 --- a/lib/index.js +++ b/lib/index.js @@ -224,6 +224,8 @@ function renderMarkdown (files, ms, done) { // render each page each(pages, (page, fname) => { + console.log(files) + console.log(page.source) const file = externalSource(page.source) ? { contents: '' } : files[page.source] const contents = file.contents.toString() From c5f1eebec603d43c6433331c63cb0382dab05fcf Mon Sep 17 00:00:00 2001 From: Ilya Radchenko Date: Tue, 5 Nov 2019 15:57:14 -0500 Subject: [PATCH 14/18] loggy --- lib/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 929caad..556a0d6 100644 --- a/lib/index.js +++ b/lib/index.js @@ -224,8 +224,7 @@ function renderMarkdown (files, ms, done) { // render each page each(pages, (page, fname) => { - console.log(files) - console.log(page.source) + console.log(files, page.source) const file = externalSource(page.source) ? { contents: '' } : files[page.source] const contents = file.contents.toString() From f6f13ace3d4f987ccb57dd7522e816631983f096 Mon Sep 17 00:00:00 2001 From: Ilya Radchenko Date: Tue, 5 Nov 2019 16:04:45 -0500 Subject: [PATCH 15/18] fix index paths --- lib/index.js | 2 +- lib/indexify.js | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index 556a0d6..4c97e90 100644 --- a/lib/index.js +++ b/lib/index.js @@ -224,7 +224,7 @@ function renderMarkdown (files, ms, done) { // render each page each(pages, (page, fname) => { - console.log(files, page.source) + // console.log(files) const file = externalSource(page.source) ? { contents: '' } : files[page.source] const contents = file.contents.toString() diff --git a/lib/indexify.js b/lib/indexify.js index a0e9f46..2e63e00 100644 --- a/lib/indexify.js +++ b/lib/indexify.js @@ -1,3 +1,5 @@ +const normalizePath = require('./helpers/normalize_path') + /** * Turns a TOC into an index. * @@ -15,8 +17,9 @@ module.exports = function indexify (toc) { function walk (item, rootTitle) { if (item.url) { - sources[item.source] = item.url - index[item.url] = { + const url = normalizePath(item.url) + sources[item.source] = url + index[url] = { source: item.source, title: rootTitle !== item.title ? `${item.title} - ${rootTitle}` : item.title, pageTitle: item.title, From d2e3e0659a9e20516d0753d0dacb16e7bde2c162 Mon Sep 17 00:00:00 2001 From: Ilya Radchenko Date: Tue, 5 Nov 2019 16:11:52 -0500 Subject: [PATCH 16/18] page source npormalized --- lib/index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 4c97e90..c5c9db5 100644 --- a/lib/index.js +++ b/lib/index.js @@ -7,6 +7,7 @@ const cheerio = require('cheerio') const tocify = require('./tocify') const indexify = require('./indexify') const fixHtml = require('./fix_html') +const normalizePath = require('./helpers/normalize_path') const stripMarkdown = require('./helpers/strip_markdown') const md = require('./helpers/markdown') const memoize = require('./memoize') @@ -225,7 +226,8 @@ function renderMarkdown (files, ms, done) { // render each page each(pages, (page, fname) => { // console.log(files) - const file = externalSource(page.source) ? { contents: '' } : files[page.source] + const pageSource = normalizePath(page.source) + const file = externalSource(page.source) ? { contents: '' } : files[pageSource] const contents = file.contents.toString() const mdOptions = ms.metadata().markdown @@ -249,7 +251,7 @@ function renderMarkdown (files, ms, done) { file.markdown = file.contents file.html = $.html() file.title = page.title - file.source = page.source + file.source = pageSource file.slug = page.slug file.contents = file.html }) From 62ce19f762a266e49719c30ca5bba3f53eeca397 Mon Sep 17 00:00:00 2001 From: Ilya Radchenko Date: Wed, 6 Nov 2019 10:07:40 -0500 Subject: [PATCH 17/18] pathify before absoluteify --- lib/tocify.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/tocify.js b/lib/tocify.js index e369b7e..6167a1d 100644 --- a/lib/tocify.js +++ b/lib/tocify.js @@ -138,12 +138,12 @@ class Tocify { // `anchor`: Adds anchor (if needed) assign(item, anchorize(item.source)) - // `source`: Takes care of relative (../) paths - assign(item, absolutify(item.source, this.docs)) - // `source`: Handle OS specific paths assign(item, pathify(item.source)) + // `source`: Takes care of relative (../) paths + assign(item, absolutify(item.source, this.docs)) + if (this.sources[item.source]) { // If this same item exists before, reuse its URL and stuff const previous = this.sources[item.source] From 8b1f85ba846d3f9d5b4caeb005d05047e0f9c0d7 Mon Sep 17 00:00:00 2001 From: Ilya Radchenko Date: Mon, 16 Dec 2019 08:19:21 -0800 Subject: [PATCH 18/18] fix: more windows changes --- lib/fix_html.js | 11 +++++++---- lib/tocify.js | 1 + test/tocify/relative_test.js | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/fix_html.js b/lib/fix_html.js index ad7d10a..90e0d58 100644 --- a/lib/fix_html.js +++ b/lib/fix_html.js @@ -2,9 +2,9 @@ /* eslint-disable no-cond-assign */ const slugify = require('slugify') -const resolve = require('path').resolve -const dirname = require('path').dirname -const relative = require('path').relative +const resolve = require('path').posix.resolve +const dirname = require('path').posix.dirname +const relative = require('path').posix.relative const normalizePath = require('./helpers/normalize_path') /** @@ -58,10 +58,12 @@ function fixReferences ($, fname, sources, files, page) { } // Get the target source file it points to (eg, `docs/usage.md`). + debugger var target = normalizePath(getTarget(origUrl, base)) // Ensure that it's available. if (!sources[target]) { + debugger console.log(sources) throw new Error(`${base}: Unknown reference '${origUrl}' (target: ${target})`) } @@ -83,6 +85,7 @@ function getTarget (url, base) { if (url.substr(0, 1) === '/') { return url.substr(1) } else { - return resolve('/' + dirname(base) + '/' + url).substr(1) + let resolved = resolve('/' + dirname(base) + '/' + url).substr(1) + return resolved } } diff --git a/lib/tocify.js b/lib/tocify.js index 6167a1d..840d814 100644 --- a/lib/tocify.js +++ b/lib/tocify.js @@ -232,6 +232,7 @@ function absolutify (source, root) { if (source.substr(0, 1) !== '/') { source = normalize(root + '/' + source) + source = normalizePath(source) } source = source.replace(/^\//, '') return { source } diff --git a/test/tocify/relative_test.js b/test/tocify/relative_test.js index e9e8076..7590d2a 100644 --- a/test/tocify/relative_test.js +++ b/test/tocify/relative_test.js @@ -6,6 +6,7 @@ describe('tocify: relative', function () { let output it('handles relative URLs', function () { + debugger output = tocify([ '* [Readme](../README.md)', '* [Install](install.md)'