diff --git a/packages/kamado/src/files/file.ts b/packages/kamado/src/files/file.ts index 8e3767d..b0d019d 100644 --- a/packages/kamado/src/files/file.ts +++ b/packages/kamado/src/files/file.ts @@ -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; @@ -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); +} diff --git a/packages/kamado/src/files/index.ts b/packages/kamado/src/files/index.ts index c806541..98bd81e 100644 --- a/packages/kamado/src/files/index.ts +++ b/packages/kamado/src/files/index.ts @@ -1,2 +1,3 @@ export { getFileContent } from './file-content.js'; +export { getFile, urlToFile } from './file.js'; export type { CompilableFile, FileObject } from './types.js'; diff --git a/yarn.lock b/yarn.lock index 2ecd407..87cd5d7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"