-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
55 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module.exports = class Navigation { | ||
constructor({ baseURI }) { | ||
this.baseURI = baseURI; | ||
} | ||
|
||
generate(tree) { | ||
let nodes = tree.children; | ||
if(!nodes) { | ||
return ""; | ||
} | ||
|
||
let result = []; | ||
|
||
for(let element of nodes.values()) { | ||
result.push(this.item(element, this.generate(element))); | ||
} | ||
|
||
return this.list(result); | ||
} | ||
|
||
// generate the markup for a node and its rendered children, overwrite at will | ||
item(node, children) { | ||
return `<li><a href="${this.baseURI}${node.slug}">${node.title}</a>${children}</li>`; | ||
} | ||
|
||
// generate the markup for a list of nodes, overwrite at will | ||
list(renderedNodes) { | ||
return `<ul>${renderedNodes.join("")}</ul>`; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* global describe, it */ | ||
let generatePageTree = require("../lib/tree"); | ||
let Navigation = require("../lib/navigation"); | ||
let { assertSame, fixturesPath, fixturesDir } = require("./util"); | ||
|
||
describe("navigation", () => { | ||
it("should generate a navigation", async () => { | ||
let descriptors = require(fixturesPath("pages.js")); | ||
let tree = generatePageTree(descriptors, fixturesDir); | ||
await Promise.all(tree.map(async page => page.load())); | ||
|
||
let navigation = new Navigation({ baseURI: "/" }); | ||
let result = navigation.generate(tree); | ||
assertSame(result, '<ul><li><a href="/">My Style Guide</a></li><li><a href="/atoms">Atoms</a><ul><li><a href="/atoms/button">Button</a></li><li><a href="/atoms/strong">strong</a></li></ul></li></ul>'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters