Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #151 from jamesramsay/relative-override
Browse files Browse the repository at this point in the history
Fix: override path is relative to source (fixes #150)
  • Loading branch information
James Ramsay committed Jan 4, 2016
2 parents c31f390 + 657f22d commit 2e87f9e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hercule",
"version": "2.0.2",
"version": "2.0.3",
"description": "Markdown, API Blueprint and string transclusion",
"main": "./lib/hercule",
"scripts": {
Expand Down
21 changes: 13 additions & 8 deletions src/resolve-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ export default function ResolveStream(grammar, opt, log = DEFAULT_LOG) {
const options = _.merge({}, DEFAULT_OPTIONS, opt);


function resolve(unresolvedLink, references = [], relativePath = '') {
function resolve(unresolvedLink, references, relativePath) {
let link = unresolvedLink.primary;
const fallback = unresolvedLink.fallback;
const override = _.find(references, { 'placeholder': link.href }) || fallback;
const override = _.find(references, { 'placeholder': link.href });

if (override) {
link = _.pick(override, ['href', 'hrefType']);
if (override || fallback) {
link = _.pick(override || fallback, ['href', 'hrefType']);
}

if (link.hrefType === 'file') {
if (!override && link.hrefType === 'file') {
link.href = path.join(relativePath, link.href);
}

Expand All @@ -49,8 +49,8 @@ export default function ResolveStream(grammar, opt, log = DEFAULT_LOG) {

function transform(chunk, encoding, cb) {
const rawLink = _.get(chunk, options.input);
const relativePath = _.get(chunk, `relativePath`);
const parentRefs = _.get(chunk, `references`);
const relativePath = _.get(chunk, `relativePath`) || '';
const parentRefs = _.get(chunk, `references`) || [];
let link;
let references;

Expand All @@ -68,7 +68,12 @@ export default function ResolveStream(grammar, opt, log = DEFAULT_LOG) {
return cb();
}

references = _.unique([...link.references, ...parentRefs], true);
references = _.map(link.references, (ref) => {
if (ref.hrefType === 'file') ref.href = path.join(relativePath, ref.href);
return ref;
});

references = _.unique([...references, ...parentRefs], true);
link = resolve(link, parentRefs, relativePath);

this.push(_.assign(chunk, { [options.output]: link }, { references }));
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/local-folder-nesting/common/jackdaws.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Jackdaws love my :[size link](size.md) sphinx of quartz.
Jackdaws love my :[size link](size.md) sphinx of :[material](material || "gold").
1 change: 1 addition & 0 deletions test/fixtures/local-folder-nesting/common/quartz.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
quartz
2 changes: 1 addition & 1 deletion test/fixtures/local-folder-nesting/index.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
:[Jackdaws](common/jackdaws.md)
:[Jackdaws](common/jackdaws.md material:common/quartz.md)
38 changes: 29 additions & 9 deletions test/units/resolve-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ test.cb('should handle no input', (t) => {
test.cb('should skip input without link', (t) => {
const input = {
content: 'The quick brown fox jumps over the lazy dog./n',
references: [],
};
const testStream = new ResolveStream(grammar);

Expand All @@ -46,7 +45,6 @@ test.cb('should skip input without link', (t) => {
test.cb('should parse input simple link', (t) => {
const input = {
content: 'The quick brown :[](animal.md) jumps over the lazy dog./n',
references: [],
link: {
href: 'animal.md',
},
Expand Down Expand Up @@ -118,21 +116,35 @@ test.cb('should parse input with overriding link', (t) => {
test.cb('should parse input with fallback link', (t) => {
const input = {
content: 'The quick brown :[](animal) jumps over the lazy dog./n',
references: [],
link: {
href: 'animal || "fox" feline:cat.md food:cheese.md',
},
};
const expected = {
href: 'fox',
hrefType: 'string',
content: 'The quick brown :[](animal) jumps over the lazy dog./n',
references: [
{
placeholder: 'feline',
href: 'cat.md',
hrefType: 'file',
},
{
placeholder: 'food',
href: 'cheese.md',
hrefType: 'file',
},
],
link: {
href: 'fox',
hrefType: 'string',
},
};
const testStream = new ResolveStream(grammar);

testStream.on('readable', function read() {
let chunk = null;
while ((chunk = this.read()) !== null) {
t.same(chunk.link, expected);
t.same(chunk, expected);
}
});

Expand Down Expand Up @@ -173,18 +185,26 @@ test.cb('should handle parse error', (t) => {
test.cb('should resolve link relative to file', (t) => {
const input = {
content: ':[](animal.md)',
link: {
href: 'animal.md',
},
relativePath: 'foo',
};
const expected = {
href: 'foo/animal.md',
hrefType: 'file',
content: ':[](animal.md)',
references: [],
link: {
href: 'foo/animal.md',
hrefType: 'file',
},
relativePath: 'foo',
};
const testStream = new ResolveStream(grammar);

testStream.on('readable', function read() {
let chunk = null;
while ((chunk = this.read()) !== null) {
t.same(chunk.link, input.link);
t.same(chunk, expected);
}
});

Expand Down

0 comments on commit 2e87f9e

Please sign in to comment.