Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change compile option to files #103

Merged
merged 6 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions workspace/marqua/src/fs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -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<Output & import('../types.js').Metadata>]
*
* @param {Options} options
* @param {(chunk: import('../types.js').HydrateChunk) => undefined | Output} [hydrate]
* @param {(chunk: import('../types.js').HydrateChunk) => undefined | Output} hydrate
* @param {(items: Array<Output & import('../types.js').Metadata>) => 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),
) {
Expand All @@ -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 [];
});
Expand Down
2 changes: 2 additions & 0 deletions workspace/marqua/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { marker } from './artisan/index.js';
import type { parse } from './core/index.js';

type Primitives = string | boolean | null;
Expand All @@ -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<
Expand Down
13 changes: 11 additions & 2 deletions workspace/marqua/test/apps/multiple/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand Down
1 change: 1 addition & 0 deletions workspace/marqua/test/apps/utils.js
Original file line number Diff line number Diff line change
@@ -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'));
Expand Down
2 changes: 1 addition & 1 deletion workspace/marqua/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"target": "ES2019",
"noEmit": true
},
"include": ["src/**/*"],
"include": ["src/**/*", "test/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
}