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: diff --git a/lib/fix_html.js b/lib/fix_html.js index 89ba555..90e0d58 100644 --- a/lib/fix_html.js +++ b/lib/fix_html.js @@ -2,9 +2,10 @@ /* 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') /** * 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,11 +58,14 @@ function fixReferences ($, fname, sources, files, page) { } // Get the target source file it points to (eg, `docs/usage.md`). - var target = getTarget(origUrl, base) + debugger + var target = normalizePath(getTarget(origUrl, base)) // Ensure that it's available. if (!sources[target]) { - throw new Error(`${base}: Unknown reference '${origUrl}'`) + debugger + console.log(sources) + throw new Error(`${base}: Unknown reference '${origUrl}' (target: ${target})`) } // Relativize that absolute URL. @@ -81,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/helpers/normalize_path.js b/lib/helpers/normalize_path.js new file mode 100644 index 0000000..bc7d2f2 --- /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 +} diff --git a/lib/index.js b/lib/index.js index 4befef4..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') @@ -224,7 +225,9 @@ function renderMarkdown (files, ms, done) { // render each page each(pages, (page, fname) => { - const file = externalSource(page.source) ? { contents: '' } : files[page.source] + // console.log(files) + const pageSource = normalizePath(page.source) + const file = externalSource(page.source) ? { contents: '' } : files[pageSource] const contents = file.contents.toString() const mdOptions = ms.metadata().markdown @@ -248,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 }) 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, diff --git a/lib/tocify.js b/lib/tocify.js index 479b55b..840d814 100644 --- a/lib/tocify.js +++ b/lib/tocify.js @@ -4,8 +4,10 @@ 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 log = require('./log') const slugify = require('./slugify') const tocifyPage = require('./tocify_page') const externalSource = require('./external_source') @@ -91,6 +93,8 @@ class Tocify { } }) + log(`tocify(run): sources ${JSON.stringify(this.sources)}`) + return re } @@ -134,6 +138,9 @@ class Tocify { // `anchor`: Adds anchor (if needed) assign(item, anchorize(item.source)) + // `source`: Handle OS specific paths + assign(item, pathify(item.source)) + // `source`: Takes care of relative (../) paths assign(item, absolutify(item.source, this.docs)) @@ -158,6 +165,8 @@ class Tocify { if (headings) item.headings = headings } + log(`tocify(itemify): item ${JSON.stringify(item)}`) + return item } @@ -223,7 +232,24 @@ function absolutify (source, root) { if (source.substr(0, 1) !== '/') { source = normalize(root + '/' + source) + source = normalizePath(source) } 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 = normalizePath(source) + return { source } +} 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)") }) }) 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)'