Skip to content

Commit

Permalink
Merge branch 'Luke-zhang-04-sort-filetree'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ole Eskild Steensen committed Jan 25, 2023
2 parents 5d9b0e5 + bc2584b commit 2a5fa33
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/site/_data/filetree.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,41 @@ const fs = require('fs');
module.exports = async () => {
const tree = await fsFileTree("src/site/notes");
populateWithPermalink(tree);


return sortTree(tree);
}

const sortTree = (unsorted) => {
//Sort by folder before file, then by name
const orderedTree = Object.keys(tree).sort((a,b)=>{
if(a.indexOf(".md") > -1 && b.indexOf(".md") === -1){
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){
if (a.indexOf(".md") === -1 && b.indexOf(".md") > -1) {
return -1;
}

if(a>b){
if (a.toLowerCase() > b.toLowerCase()) {
return 1;
}

return -1;
}).reduce(
(obj, key) => {
obj[key] = tree[key];
obj[key] = unsorted[key];

return obj;
},
{}
);

for (const key of Object.keys(orderedTree)) {
if (!orderedTree[key].path) {
orderedTree[key] = sortTree(orderedTree[key])
}
}

return orderedTree;
}

Expand All @@ -40,14 +52,14 @@ function getPermalinkAndName(path, key) {
if (frontMatter.data.permalink) {
permalink = frontMatter.data.permalink;
}
if (frontMatter.data.title){
if (frontMatter.data.title) {
name = frontMatter.data.title
}
} catch {
//ignore
}

return {permalink, name};
return { permalink, name };
}

function populateWithPermalink(tree) {
Expand Down

0 comments on commit 2a5fa33

Please sign in to comment.