Skip to content

Commit

Permalink
Merge f6f13ac into 0c48454
Browse files Browse the repository at this point in the history
  • Loading branch information
knownasilya committed Nov 5, 2019
2 parents 0c48454 + f6f13ac commit 6330133
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 7 deletions.
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
8 changes: 5 additions & 3 deletions lib/fix_html.js
Original file line number Diff line number Diff line change
Expand Up @@ -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).
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,12 @@ 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]) {
throw new Error(`${base}: Unknown reference '${origUrl}'`)
console.log(sources)
throw new Error(`${base}: Unknown reference '${origUrl}' (target: ${target})`)
}

// Relativize that absolute URL.
Expand Down
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
}
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ 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 contents = file.contents.toString()
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
25 changes: 25 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 @@ -137,6 +141,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]
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 @@ -227,3 +236,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 = 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)")
})
})

0 comments on commit 6330133

Please sign in to comment.