Skip to content
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
27 changes: 26 additions & 1 deletion packages/kamado/src/files/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import type { CompilableFile } from './types.js';

import path from 'node:path';

import { urlToLocalPath } from '@d-zero/shared/url-to-local-path';
import grayMatter from 'gray-matter';

import { computeOutputPath } from '../path/output-path.js';

import { getFileContent } from './file-content.js';

interface GetFileOptions {
export interface GetFileOptions {
readonly inputDir: string;
readonly outputDir: string;
readonly outputExtension: string;
Expand Down Expand Up @@ -81,3 +82,27 @@ export function getFile(filePath: string, options: GetFileOptions): CompilableFi
},
};
}

/**
* Creates a CompilableFile object from a URL
* @param url - URL path (e.g., '/about/' or '/products/item.html')
* @param options - Options for getting the file
* @param options.inputDir - Input directory path
* @param options.outputDir - Output directory path
* @param options.outputExtension - Output file extension (e.g., '.html')
* @returns CompilableFile object
* @example
* ```typescript
* const file = urlToFile('/about/', {
* inputDir: './src',
* outputDir: './dist',
* outputExtension: '.html',
* });
* ```
*/
export function urlToFile(url: string | URL, options: GetFileOptions): CompilableFile {
const urlString = typeof url === 'string' ? url : url.href;
const relativePath = urlToLocalPath(urlString, options.outputExtension);
const filePath = path.resolve(options.inputDir, relativePath);
return getFile(filePath, options);
}
1 change: 1 addition & 0 deletions packages/kamado/src/files/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { getFileContent } from './file-content.js';
export { getFile, urlToFile } from './file.js';
export type { CompilableFile, FileObject } from './types.js';
13 changes: 0 additions & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1029,19 +1029,6 @@ __metadata:
languageName: node
linkType: hard

"@d-zero/shared@npm:0.17.0":
version: 0.17.0
resolution: "@d-zero/shared@npm:0.17.0"
dependencies:
"@holiday-jp/holiday_jp": "npm:2.5.1"
await-event-emitter: "npm:2.0.2"
dayjs: "npm:1.11.19"
escape-string-regexp: "npm:5.0.0"
front-matter: "npm:4.0.2"
checksum: 10c0/59597bce3f193f141e0ae28a5b1659ea778ae2490427383ccc1dcae687ea4b06c6be44c2428dac0d97fa26dacdf0bb8c72743b751434ea791b67397d7529618b
languageName: node
linkType: hard

"@d-zero/shared@npm:0.17.1":
version: 0.17.1
resolution: "@d-zero/shared@npm:0.17.1"
Expand Down