-
Notifications
You must be signed in to change notification settings - Fork 1
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
12 changed files
with
106 additions
and
95 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
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
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,7 +1,7 @@ | ||
import { slug } from "github-slugger" | ||
import deburr from "lodash.deburr" | ||
import { slug } from 'github-slugger' | ||
import deburr from 'lodash.deburr' | ||
|
||
const mkname = (raw) => { | ||
return deburr(slug(raw)) | ||
}; | ||
} | ||
export { mkname } |
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,35 +1,41 @@ | ||
|
||
/** | ||
* Sort documents based on slug, lang and sort attributes. | ||
*/ | ||
|
||
export default function pluginMdxSort (documents) { | ||
export default function pluginMdxSort(documents) { | ||
return documents | ||
.sort( (a, b) => a.slug.length - b.slug.length ) | ||
.sort( (a, b) => { | ||
.sort((a, b) => a.slug.length - b.slug.length) | ||
.sort((a, b) => { | ||
// Sort by collection | ||
if (a.collection !== b.collection) return a.collection < b.collection ? -1 : 0 | ||
if (a.collection !== b.collection) | ||
return a.collection < b.collection ? -1 : 0 | ||
// Sort by lang | ||
if (a.lang !== b.lang) return a.lang < b.lang ? -1 : 0 | ||
// Keep original order of a and b | ||
if (a.slug.length !== b.slug.length) return 0 | ||
// Compare 2 identical parent slugs with the sort attribute | ||
const min = Math.min(a.slug.length, b.slug.length) | ||
if(min === 0){ | ||
if (min === 0) { | ||
return -1 | ||
} | ||
const slugA = [...a.slug.slice(0, a.slug.length-1), a.sort ?? a.slug[a.slug.length-1]] | ||
const slugB = [...b.slug.slice(0, b.slug.length-1), b.sort ?? b.slug[b.slug.length-1]] | ||
const slugA = [ | ||
...a.slug.slice(0, a.slug.length - 1), | ||
a.sort ?? a.slug[a.slug.length - 1], | ||
] | ||
const slugB = [ | ||
...b.slug.slice(0, b.slug.length - 1), | ||
b.sort ?? b.slug[b.slug.length - 1], | ||
] | ||
for (let i = 0; i < min; i++) { | ||
// Last element, use the sort key | ||
const itemA = slugA[i] | ||
const itemB = slugB[i] | ||
// Keep going if parent is shared | ||
if(itemA === itemB){ | ||
if (itemA === itemB) { | ||
continue | ||
} | ||
// Diverging parent | ||
return itemA > itemB ? 1 : - 1 | ||
return itemA > itemB ? 1 : -1 | ||
} | ||
}) | ||
} |
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,58 +1,61 @@ | ||
import { mutate } from 'mixme' | ||
|
||
const getIndexInTree = (tree, el) => { | ||
for(let i = 0; i < tree.length; i++){ | ||
for (let i = 0; i < tree.length; i++) { | ||
const doc = tree[i] | ||
if(doc.slug[doc.slug.length-1] === el) return i | ||
if (doc.slug[doc.slug.length - 1] === el) return i | ||
} | ||
} | ||
|
||
/** | ||
* Organize document as a tree using slug and slug_relative attribute and | ||
* placing child documents inside a children attribute. | ||
*/ | ||
export default function tree (documents) { | ||
let tree = []; | ||
export default function tree(documents) { | ||
let tree = [] | ||
let root = undefined | ||
documents | ||
// Find comment root | ||
.map((document) => { | ||
if(root === undefined){ | ||
// root = document.slug | ||
root = document.slug.slice(0, document.slug.length-1) | ||
} | ||
for(const i in root){ | ||
if(root[i] !== document.slug[i]){ | ||
root = root.slice(0, i) | ||
// Find comment root | ||
.map((document) => { | ||
if (root === undefined) { | ||
root = document.slug.slice(0, document.slug.length - 1) | ||
} | ||
} | ||
return document | ||
}) | ||
// Strip slug from root | ||
.map((document) => { | ||
return {...document, slug_relative: document.slug.slice(root.length, document.slug.length)} | ||
}) | ||
// Build the tree | ||
.map((document) => { | ||
let ltree = tree | ||
for(let i=0; i<document.slug_relative.length; i++){ | ||
let treeIndex = getIndexInTree(ltree, document.slug_relative[i]) | ||
// Document not yet inside the tree | ||
if (treeIndex === undefined){ | ||
treeIndex = ltree.length | ||
ltree.push({ | ||
children: [], | ||
slug: [...root, ...document.slug_relative.slice(0, i + 1)], | ||
slug_relative: document.slug_relative.slice(0, i + 1) | ||
}) | ||
for (const i in root) { | ||
if (root[i] !== document.slug[i]) { | ||
root = root.slice(0, i) | ||
} | ||
} | ||
// Document | ||
if(i == document.slug_relative.length - 1){ | ||
mutate(ltree[treeIndex], document) | ||
} else { // Parent | ||
ltree = ltree[treeIndex].children | ||
return document | ||
}) | ||
// Strip slug from root | ||
.map((document) => { | ||
return { | ||
...document, | ||
slug_relative: document.slug.slice(root.length, document.slug.length), | ||
} | ||
} | ||
}) | ||
}) | ||
// Build the tree | ||
.map((document) => { | ||
let ltree = tree | ||
for (let i = 0; i < document.slug_relative.length; i++) { | ||
let treeIndex = getIndexInTree(ltree, document.slug_relative[i]) | ||
// Document not yet inside the tree | ||
if (treeIndex === undefined) { | ||
treeIndex = ltree.length | ||
ltree.push({ | ||
children: [], | ||
slug: [...root, ...document.slug_relative.slice(0, i + 1)], | ||
slug_relative: document.slug_relative.slice(0, i + 1), | ||
}) | ||
} | ||
// Document | ||
if (i == document.slug_relative.length - 1) { | ||
mutate(ltree[treeIndex], document) | ||
} else { | ||
// Parent | ||
ltree = ltree[treeIndex].children | ||
} | ||
} | ||
}) | ||
return tree | ||
} |