From 2bd83a62a9870b0c04f20110a2bebf03e349de86 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Thu, 4 Jan 2024 17:27:33 +0700 Subject: [PATCH] feat: change `compile` option to `files` (#103) --- workspace/marqua/src/fs/index.js | 18 +++++++----------- workspace/marqua/src/types.d.ts | 2 ++ .../marqua/test/apps/multiple/index.spec.js | 13 +++++++++++-- workspace/marqua/test/apps/utils.js | 1 + workspace/marqua/tsconfig.json | 2 +- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/workspace/marqua/src/fs/index.js b/workspace/marqua/src/fs/index.js index 6dfcc46..4cc659e 100644 --- a/workspace/marqua/src/fs/index.js +++ b/workspace/marqua/src/fs/index.js @@ -24,7 +24,7 @@ export function compile(entry, hydrate) { const buffer = fs.readFileSync(path); return { type: 'file', name, path, buffer }; }); - return hydrate({ breadcrumb, buffer, parse, siblings: tree }); + return hydrate({ breadcrumb, buffer, marker, parse, siblings: tree }); } const { content, metadata } = parse(buffer.toString('utf-8')); return { ...metadata, content }; @@ -41,19 +41,19 @@ export function compile(entry, hydrate) { /** * @template {{ * entry: string; - * compile?(path: string): boolean; * depth?: number; + * files?(path: string): boolean; * }} Options * @template {object} Output * @template [Transformed = Array] * * @param {Options} options - * @param {(chunk: import('../types.js').HydrateChunk) => undefined | Output} [hydrate] + * @param {(chunk: import('../types.js').HydrateChunk) => undefined | Output} hydrate * @param {(items: Array) => Transformed} [transform] * @returns {Transformed} */ export function traverse( - { entry, compile: fn = (v) => v.endsWith('.md'), depth: level = 0 }, + { entry, depth: level = 0, files = (v) => v.endsWith('.md') }, hydrate, transform = (v) => /** @type {Transformed} */ (v), ) { @@ -73,16 +73,12 @@ export function traverse( }); const backpack = tree.flatMap(({ type, path, buffer }) => { - if (type === 'file') { - const data = fn(path) && compile(path, hydrate); - if (data && Object.keys(data).length) return data; - if (!hydrate) return []; // skip this file + if (type === 'file' && files(path)) { const breadcrumb = path.split(/[/\\]/).reverse(); - return hydrate({ breadcrumb, buffer, parse, siblings: tree }) ?? []; + return hydrate({ breadcrumb, buffer, marker, parse, siblings: tree }) ?? []; } else if (level !== 0) { const depth = level < 0 ? level : level - 1; - const options = { entry: path, depth, compile: fn }; - return traverse(options, hydrate); + return traverse({ entry: path, depth, files }, hydrate); } return []; }); diff --git a/workspace/marqua/src/types.d.ts b/workspace/marqua/src/types.d.ts index 44f2b07..8e07773 100644 --- a/workspace/marqua/src/types.d.ts +++ b/workspace/marqua/src/types.d.ts @@ -1,3 +1,4 @@ +import type { marker } from './artisan/index.js'; import type { parse } from './core/index.js'; type Primitives = string | boolean | null; @@ -9,6 +10,7 @@ export interface FrontMatter { export interface HydrateChunk { breadcrumb: string[]; buffer: Buffer; + marker: typeof marker; parse: typeof parse; // TODO: remove self from siblings siblings: Array< diff --git a/workspace/marqua/test/apps/multiple/index.spec.js b/workspace/marqua/test/apps/multiple/index.spec.js index ecc8643..d044c6a 100644 --- a/workspace/marqua/test/apps/multiple/index.spec.js +++ b/workspace/marqua/test/apps/multiple/index.spec.js @@ -11,7 +11,10 @@ const basics = { const target = `${process.cwd()}/test/apps/multiple`; basics.standard('standard traversal', () => { - const output = traverse({ entry: `${target}/standard/input` }); + const output = traverse({ entry: `${target}/standard/input` }, ({ buffer, marker, parse }) => { + const { content, metadata } = parse(buffer.toString('utf-8')); + return { ...metadata, content: marker.render(content) }; + }); const expected = readJSON(`${target}/standard/expected.json`); assert.type(output, 'object'); @@ -21,7 +24,13 @@ basics.standard('standard traversal', () => { }); basics.depth('depth traversal', () => { - const output = traverse({ entry: `${target}/depth/input`, depth: 1 }); + const output = traverse( + { entry: `${target}/depth/input`, depth: 1 }, + ({ buffer, marker, parse }) => { + const { content, metadata } = parse(buffer.toString('utf-8')); + return { ...metadata, content: marker.render(content) }; + }, + ); const expected = readJSON(`${target}/depth/expected.json`); assert.type(output, 'object'); diff --git a/workspace/marqua/test/apps/utils.js b/workspace/marqua/test/apps/utils.js index d3956a5..b2b3802 100644 --- a/workspace/marqua/test/apps/utils.js +++ b/workspace/marqua/test/apps/utils.js @@ -1,6 +1,7 @@ import * as fs from 'fs'; import * as path from 'path'; +/** @param {string} pathname */ export function readJSON(pathname) { if (path.sep !== '/') pathname = pathname.replace(/\//g, path.sep); return JSON.parse(fs.readFileSync(pathname, 'utf-8')); diff --git a/workspace/marqua/tsconfig.json b/workspace/marqua/tsconfig.json index 1edb389..08ffa81 100644 --- a/workspace/marqua/tsconfig.json +++ b/workspace/marqua/tsconfig.json @@ -4,6 +4,6 @@ "target": "ES2019", "noEmit": true }, - "include": ["src/**/*"], + "include": ["src/**/*", "test/**/*"], "exclude": ["node_modules", "**/*.spec.ts"] }