Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: toc paths #203

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ os:
- windows
- linux
node_js:
- 'stable'
- 'node'
- '10'
- '8'
cache:
Expand Down
19 changes: 12 additions & 7 deletions lib/fix_html.js
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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 () {
Expand All @@ -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.
Expand All @@ -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
}
}
9 changes: 9 additions & 0 deletions lib/helpers/normalize_path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = function fixSlashes (url) {
if (!url) {
return url
}
url = url.replace(/\\\\/g, '/')
.replace(/\\/g, '/')

return url
}
7 changes: 5 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -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
})
Expand Down
7 changes: 5 additions & 2 deletions lib/indexify.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const normalizePath = require('./helpers/normalize_path')

/**
* Turns a TOC into an index.
*
Expand All @@ -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,
Expand Down
26 changes: 26 additions & 0 deletions lib/tocify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -91,6 +93,8 @@ class Tocify {
}
})

log(`tocify(run): sources ${JSON.stringify(this.sources)}`)

return re
}

Expand Down Expand Up @@ -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))

Expand All @@ -158,6 +165,8 @@ class Tocify {
if (headings) item.headings = headings
}

log(`tocify(itemify): item ${JSON.stringify(item)}`)

return item
}

Expand Down Expand Up @@ -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 }
}
2 changes: 1 addition & 1 deletion test/index/invalid_refs_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
})
})
1 change: 1 addition & 0 deletions test/tocify/relative_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ describe('tocify: relative', function () {
let output

it('handles relative URLs', function () {
debugger
output = tocify([
'* [Readme](../README.md)',
'* [Install](install.md)'
Expand Down