-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'uroybd-maturity-support'
- Loading branch information
Showing
13 changed files
with
212 additions
and
82 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
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 |
---|---|---|
@@ -1,80 +1,87 @@ | ||
const fsFileTree = require("fs-file-tree"); | ||
const matter = require('gray-matter'); | ||
const fs = require('fs'); | ||
const matter = require("gray-matter"); | ||
const fs = require("fs"); | ||
|
||
module.exports = async () => { | ||
const tree = await fsFileTree("src/site/notes"); | ||
populateWithPermalink(tree); | ||
const tree = await fsFileTree("src/site/notes"); | ||
populateWithPermalink(tree); | ||
|
||
return sortTree(tree); | ||
} | ||
return sortTree(tree); | ||
}; | ||
|
||
const sortTree = (unsorted) => { | ||
//Sort by folder before file, then by name | ||
const orderedTree = Object.keys(unsorted).sort((a, b) => { | ||
if (a.indexOf(".md") > -1 && b.indexOf(".md") === -1) { | ||
return 1; | ||
} | ||
//Sort by folder before file, then by name | ||
const orderedTree = Object.keys(unsorted) | ||
.sort((a, b) => { | ||
if (a.indexOf(".md") > -1 && b.indexOf(".md") === -1) { | ||
return 1; | ||
} | ||
|
||
if (a.indexOf(".md") === -1 && b.indexOf(".md") > -1) { | ||
return -1; | ||
} | ||
if (a.indexOf(".md") === -1 && b.indexOf(".md") > -1) { | ||
return -1; | ||
} | ||
|
||
if (a.toLowerCase() > b.toLowerCase()) { | ||
return 1; | ||
} | ||
if (a.toLowerCase() > b.toLowerCase()) { | ||
return 1; | ||
} | ||
|
||
return -1; | ||
}).reduce( | ||
(obj, key) => { | ||
obj[key] = unsorted[key]; | ||
return -1; | ||
}) | ||
.reduce((obj, key) => { | ||
obj[key] = unsorted[key]; | ||
|
||
return obj; | ||
}, | ||
{} | ||
); | ||
return obj; | ||
}, {}); | ||
|
||
for (const key of Object.keys(orderedTree)) { | ||
if (!orderedTree[key].path) { | ||
orderedTree[key] = sortTree(orderedTree[key]) | ||
} | ||
for (const key of Object.keys(orderedTree)) { | ||
if (!orderedTree[key].path) { | ||
orderedTree[key] = sortTree(orderedTree[key]); | ||
} | ||
} | ||
|
||
return orderedTree; | ||
} | ||
return orderedTree; | ||
}; | ||
|
||
function getPermalinkAndName(path, key) { | ||
let permalink = "/" | ||
let name = key.replace(".md", ""); | ||
try { | ||
const file = fs.readFileSync(`${path}`, 'utf8'); | ||
const frontMatter = matter(file); | ||
if (frontMatter.data.permalink) { | ||
permalink = frontMatter.data.permalink; | ||
} | ||
if (frontMatter.data.title) { | ||
name = frontMatter.data.title | ||
} | ||
} catch { | ||
//ignore | ||
function getPermalinkMeta(path, key) { | ||
let permalink = "/"; | ||
let name = key.replace(".md", ""); | ||
let noteIcon = process.env.NOTE_ICON_DEFAULT; | ||
try { | ||
const file = fs.readFileSync(`${path}`, "utf8"); | ||
const frontMatter = matter(file); | ||
if (frontMatter.data.permalink) { | ||
permalink = frontMatter.data.permalink; | ||
} | ||
if (frontMatter.data.title) { | ||
name = frontMatter.data.title; | ||
} | ||
if (frontMatter.data.noteIcon) { | ||
noteIcon = frontMatter.data.noteIcon; | ||
} | ||
} catch { | ||
//ignore | ||
} | ||
|
||
return { permalink, name }; | ||
return { permalink, name, noteIcon }; | ||
} | ||
|
||
function populateWithPermalink(tree) { | ||
Object.keys(tree).forEach(key => { | ||
if (tree[key].path) { | ||
const isNote = tree[key].path.endsWith(".md"); | ||
tree[key].isNote = isNote; | ||
if (isNote) { | ||
let { permalink, name } = getPermalinkAndName(tree[key].path, key); | ||
tree[key].permalink = permalink | ||
tree[key].name = name | ||
} | ||
} else { | ||
tree[key].isFolder = true; | ||
populateWithPermalink(tree[key]); | ||
} | ||
}); | ||
Object.keys(tree).forEach((key) => { | ||
if (tree[key].path) { | ||
const isNote = tree[key].path.endsWith(".md"); | ||
tree[key].isNote = isNote; | ||
if (isNote) { | ||
let { permalink, name, noteIcon } = getPermalinkMeta( | ||
tree[key].path, | ||
key | ||
); | ||
tree[key].permalink = permalink; | ||
tree[key].name = name; | ||
tree[key].noteIcon = noteIcon; | ||
} | ||
} else { | ||
tree[key].isFolder = true; | ||
populateWithPermalink(tree[key]); | ||
} | ||
}); | ||
} |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Oops, something went wrong.