From e740963887156b1910f21f9f4b348132632d9bb2 Mon Sep 17 00:00:00 2001 From: David Worms Date: Sun, 16 Jun 2024 21:40:25 +0200 Subject: [PATCH] refactor(redac): prettify --- packages/redac/lib/collection.js | 4 +- packages/redac/lib/index.js | 20 ++--- packages/redac/lib/plugin-mdx/4.parse.js | 6 +- packages/redac/lib/plugin-mdx/5.overload.js | 17 +++-- packages/redac/lib/plugin-mdx/index.js | 12 +-- packages/redac/lib/plugin-yaml/3.enrich.js | 4 - packages/redac/lib/plugin-yaml/index.js | 12 +-- packages/redac/lib/utils/filedirname.js | 7 +- packages/redac/lib/utils/mklayout.js | 4 +- packages/redac/lib/utils/slugger.js | 6 +- packages/redac/lib/utils/sort.js | 26 ++++--- packages/redac/lib/utils/tree.js | 83 +++++++++++---------- 12 files changed, 106 insertions(+), 95 deletions(-) diff --git a/packages/redac/lib/collection.js b/packages/redac/lib/collection.js index 5cd5a23..4df8873 100644 --- a/packages/redac/lib/collection.js +++ b/packages/redac/lib/collection.js @@ -5,13 +5,13 @@ export default function collection(engine, collection) { const stack = [] const promise = new Promise((resolve, reject) => { setImmediate(async () => { - try{ + try { let documents = (await engine.db())[collection] ?? [] for (const fn of stack) { documents = await fn(documents) } resolve(documents) - }catch(error){ + } catch (error) { reject(error) } }) diff --git a/packages/redac/lib/index.js b/packages/redac/lib/index.js index 037e507..7f80025 100644 --- a/packages/redac/lib/index.js +++ b/packages/redac/lib/index.js @@ -5,16 +5,16 @@ import sort from './utils/sort.js' export default function engine(plugins = []) { // Configuration normalization if (!Array.isArray(plugins)) plugins = [plugins] - plugins = plugins.map( (plugin) => { - if (typeof plugin === 'function' ) { + plugins = plugins.map((plugin) => { + if (typeof plugin === 'function') { return plugin() } else if (plugin !== null && typeof plugin === 'object') { - if(typeof plugin.plugin === 'function'){ + if (typeof plugin.plugin === 'function') { return plugin.plugin.call(null, plugin.config) } else if (plugin.plugin !== null && typeof plugin.plugin === 'object') { return plugin.plugin } else { - `REDAC_INVALID_ARGUMENTS: plugin config must be an object or a function, got ${JSON.stringify( + ;`REDAC_INVALID_ARGUMENTS: plugin config must be an object or a function, got ${JSON.stringify( plugin )}.` } @@ -29,7 +29,7 @@ export default function engine(plugins = []) { // Load the engine const engine = { plugins: plugandplay({ - plugins: plugins + plugins: plugins, }), db: async () => { const documents = [] @@ -41,8 +41,8 @@ export default function engine(plugins = []) { // Default implementation handler: ({ documents }) => { // Use the source hook to discover and return documents - } - }); + }, + }) const collections = {} // Group by collection documents.forEach((document) => { @@ -65,9 +65,9 @@ export default function engine(plugins = []) { // Expose arguments to plugins authors args: { engine }, // Default implementation - handler: ({engine}) => { + handler: ({ engine }) => { // Use the init hook to register properties to engine - } - }); + }, + }) return engine } diff --git a/packages/redac/lib/plugin-mdx/4.parse.js b/packages/redac/lib/plugin-mdx/4.parse.js index 52da108..5d61a2a 100644 --- a/packages/redac/lib/plugin-mdx/4.parse.js +++ b/packages/redac/lib/plugin-mdx/4.parse.js @@ -34,7 +34,11 @@ const imageSrc = ({ document, config }) => { } }) await each(nodes, async (node) => { - node.url = await config.image_src.call(null, {config: config, document: document, node: node}) + node.url = await config.image_src.call(null, { + config: config, + document: document, + node: node, + }) }) } } diff --git a/packages/redac/lib/plugin-mdx/5.overload.js b/packages/redac/lib/plugin-mdx/5.overload.js index edc5866..ec7b985 100644 --- a/packages/redac/lib/plugin-mdx/5.overload.js +++ b/packages/redac/lib/plugin-mdx/5.overload.js @@ -1,17 +1,20 @@ - -export default function pluginMdxOverload (plugin) { - const {documents} = plugin +export default function pluginMdxOverload(plugin) { + const { documents } = plugin // Index absolute slug found in filenames with metadata slug const slugs = {} - documents.forEach( document => { - slugs[(document.lang ?? '')+'/'+document.slug.join('/')] = document.data?.slug + documents.forEach((document) => { + slugs[(document.lang ?? '') + '/' + document.slug.join('/')] = + document.data?.slug }) // Reconstruct the slug with metadata slug if present plugin.documents = documents.map((document) => { const newSlug = [] for (let i = 0; i < document.slug.length; i++) { - const overloadedSlug = slugs[(document.lang ?? '')+'/'+document.slug.slice(0, i+1).join('/')] - if(overloadedSlug){ + const overloadedSlug = + slugs[ + (document.lang ?? '') + '/' + document.slug.slice(0, i + 1).join('/') + ] + if (overloadedSlug) { newSlug[i] = overloadedSlug } else { newSlug[i] = document.slug[i] diff --git a/packages/redac/lib/plugin-mdx/index.js b/packages/redac/lib/plugin-mdx/index.js index 0a552c2..15d83a2 100644 --- a/packages/redac/lib/plugin-mdx/index.js +++ b/packages/redac/lib/plugin-mdx/index.js @@ -6,9 +6,9 @@ import step_4_parse from './4.parse.js' import step_5_overload from './5.overload.js' const getConfigs = (config) => { - if(config == null) return [] - if(!Array.isArray(config)) config = [config] - return config.map( config => { + if (config == null) return [] + if (!Array.isArray(config)) config = [config] + return config.map((config) => { if (typeof config === 'string') { return { target: config, @@ -37,7 +37,7 @@ export default (config) => { return engine } }, - 'engine:source': async ({documents}) => + 'engine:source': async ({ documents }) => each(configs, true, async (config) => { const docs = await step_1_normalize({ config }) .then(step_2_load) @@ -46,8 +46,8 @@ export default (config) => { .then(step_5_overload) .then(({ documents }) => documents) documents.push(...docs) - }) - } + }), + }, } } diff --git a/packages/redac/lib/plugin-yaml/3.enrich.js b/packages/redac/lib/plugin-yaml/3.enrich.js index c5d51e5..a690de9 100644 --- a/packages/redac/lib/plugin-yaml/3.enrich.js +++ b/packages/redac/lib/plugin-yaml/3.enrich.js @@ -1,8 +1,4 @@ -import fs from 'node:fs/promises' -import { parse } from 'yaml' import path from 'path' -import { glob } from 'glob' -import each from 'each' export default function pluginYamlEnrich(plugin) { const { config, documents } = plugin diff --git a/packages/redac/lib/plugin-yaml/index.js b/packages/redac/lib/plugin-yaml/index.js index 6aa97b7..9f95d6b 100644 --- a/packages/redac/lib/plugin-yaml/index.js +++ b/packages/redac/lib/plugin-yaml/index.js @@ -4,9 +4,9 @@ import step_2_load from './2.load.js' import step_3_enrich from './3.enrich.js' const getConfigs = (config) => { - if(config == null) return [] - if(!Array.isArray(config)) config = [config] - return config.map( config => { + if (config == null) return [] + if (!Array.isArray(config)) config = [config] + return config.map((config) => { if (typeof config === 'string') { return { target: config, @@ -35,15 +35,15 @@ export default (config) => { return engine } }, - 'engine:source': async ({documents}) => + 'engine:source': async ({ documents }) => each(configs, true, async (config) => { const docs = await step_1_normalize({ config }) .then(step_2_load) .then(step_3_enrich) .then(({ documents }) => documents) documents.push(...docs) - }) - } + }), + }, } } diff --git a/packages/redac/lib/utils/filedirname.js b/packages/redac/lib/utils/filedirname.js index e1ff9bd..a77899b 100644 --- a/packages/redac/lib/utils/filedirname.js +++ b/packages/redac/lib/utils/filedirname.js @@ -1,6 +1,5 @@ - -import path from "path"; -import { fileURLToPath } from "url"; +import path from 'path' +import { fileURLToPath } from 'url' const dirname = (imp) => { return path.dirname(fileURLToPath(imp.meta.url)) @@ -10,4 +9,4 @@ const filename = (imp) => { throw Error('Not yet implemented') } -export {dirname, filename} +export { dirname, filename } diff --git a/packages/redac/lib/utils/mklayout.js b/packages/redac/lib/utils/mklayout.js index e48d4be..4d19fef 100644 --- a/packages/redac/lib/utils/mklayout.js +++ b/packages/redac/lib/utils/mklayout.js @@ -12,8 +12,8 @@ export default async function mklayout(tmpdir, pages) { await fs.writeFile( path_relative, frontmatter + - (content || '') + - ((content || '').endsWith('\n') ? '' : '\n') + (content || '') + + ((content || '').endsWith('\n') ? '' : '\n') ) } } diff --git a/packages/redac/lib/utils/slugger.js b/packages/redac/lib/utils/slugger.js index 9aa3a3f..3446043 100644 --- a/packages/redac/lib/utils/slugger.js +++ b/packages/redac/lib/utils/slugger.js @@ -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 } diff --git a/packages/redac/lib/utils/sort.js b/packages/redac/lib/utils/sort.js index 8c89d4b..8cd6ff6 100644 --- a/packages/redac/lib/utils/sort.js +++ b/packages/redac/lib/utils/sort.js @@ -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 } }) } diff --git a/packages/redac/lib/utils/tree.js b/packages/redac/lib/utils/tree.js index 3860de7..f8ff9cc 100644 --- a/packages/redac/lib/utils/tree.js +++ b/packages/redac/lib/utils/tree.js @@ -1,9 +1,9 @@ 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 } } @@ -11,48 +11,51 @@ const getIndexInTree = (tree, el) => { * 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 { + 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 }