Skip to content

Commit

Permalink
feat: provide a page object
Browse files Browse the repository at this point in the history
  • Loading branch information
learosema committed Oct 1, 2024
1 parent 726a2ed commit a876627
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 31 deletions.
16 changes: 7 additions & 9 deletions src/naming.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import path from 'node:path'

export function directoryNaming(dir, inputFileName, outputFileExtension = null) {
export function directoryNaming(inputFileName, outputFileExtension = null) {
const parsed = path.parse(inputFileName);
if (/\.html?$/.test(inputFileName) && parsed.name !== 'index') {
return path.join(path.normalize(dir), parsed.name, 'index.html');
return path.join('/', parsed.name, 'index.html');
}
return path.join(path.normalize(dir),
outputFileExtension ?
return path.join('/', outputFileExtension ?
inputFileName.replace(/\.\w+$/, ('.' + outputFileExtension).replace(/\.+/, '.')) : inputFileName
);
}

export function defaultNaming(dir, inputFileName, outputFileExtension = null) {
return path.join(path.normalize(dir),
outputFileExtension ?
inputFileName.replace(/\.\w+$/, ('.' + outputFileExtension).replace(/\.+/, '.')) : inputFileName
);
export function defaultNaming(inputFileName, outputFileExtension = null) {
return path.join('/', outputFileExtension ?
inputFileName.replace(/\.\w+$/, ('.' + outputFileExtension).replace(/\.+/, '.')) : inputFileName)
;
}
33 changes: 23 additions & 10 deletions src/transforms/template-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,36 +111,49 @@ export async function handleTemplateFile(config, data, inputFile) {
if (! config.extensions.has(ext)) {
return {
content,
filename: config.naming(config.dir.output, inputFile)
filename: path.normalize(path.join(config.dir.output, config.naming(inputFile)))
};
}

const plugin = config.extensions.get(ext);
const pageUrl = config.naming(inputFile, plugin?.outputFileExtension);
const absOutputFile = path.join(path.normalize(config.dir.output), pageUrl);

const { data: matterData, body } = frontmatter(content);
const fileData = Object.assign({}, structuredClone(data), matterData);
const page = {
fileSlug: parsed.name,
filePathStem: path.join(parsed.dir, parsed.name),
inputPath: inputFile,
outputPath: absOutputFile,
outputFileExtension: plugin.outputFileExtension || 'html',
rawInput: content
};

const outputFile = config.naming(config.dir.output, inputFile, plugin?.outputFileExtension);
Object.assign(fileData, {
inputFile,
outputFile,
})
Object.defineProperty(page, 'url', {value: pageUrl, writable: false});


const { data: matterData, body } = frontmatter(content);
const fileData = Object.assign({}, structuredClone(data), matterData);
if (fileData.page && typeof fileData.page === 'object') {
Object.assign(fileData.page, page);
} else {
fileData.page = page;
}

const processor = await plugin.compile(body, inputFile);

let fileContent = template(await processor(fileData))(fileData);

if (fileData.layout) {
const layoutFilePath = path.normalize(path.join(config.dir.layouts, fileData.layout));
const l = await handleTemplateFile(config,
{...fileData, content: fileContent, layout: null}, layoutFilePath);
{...fileData, content: fileContent, layout: null}, layoutFilePath, true);
if (l) {
fileContent = l.content;
} else {
throw new Error('Layout not found:' + layoutFilePath);
}
}

return {content: fileContent, filename: outputFile};
return {content: fileContent, filename: page.outputPath};
}

26 changes: 14 additions & 12 deletions tests/naming.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import { defaultNaming, directoryNaming } from '../src/naming.js';

describe('Default Naming', () => {
it('should use append-strategy for non-html files', () => {
const result = defaultNaming('test', 'logo.svg');
const result = defaultNaming('logo.svg');

assert(result === ['test', 'logo.svg'].join(path.sep));
assert.equal(result, '/logo.svg');
});

it('should still use the append-strategy for html files', () => {
const result = defaultNaming('test', 'imprint.html');
const result = defaultNaming('imprint.html');

// imprint.html -> test/imprint/index.html
assert.equal(result, ['test', 'imprint.html'].join(path.sep));
// imprint.html -> /imprint.html
assert.equal(result, '/imprint.html');
});

});
Expand All @@ -24,22 +24,24 @@ describe('Default Naming', () => {

describe('Directory Naming', () => {
it('should use append-strategy for non-html files', () => {
const result = directoryNaming('test', 'logo.svg');
const result = directoryNaming('logo.svg');

assert.equal(result, ['test', 'logo.svg'].join(path.sep));
assert.equal(result, '/logo.svg');
});

it('should use the directory-strategy for html files', () => {
const result = directoryNaming('test', 'imprint.html');
const result = directoryNaming('test/imprint.html');

// imprint.html -> test/imprint/index.html
assert.equal(result, ['test', 'imprint', 'index.html'].join(path.sep));
// imprint.html -> /test/imprint/index.html
assert.equal(result,
'/imprint/index.html'
);
});

it('should not use the directory-strategy html files named index.html', () => {
const result = directoryNaming('test', 'index.html');
const result = directoryNaming('/index.html');

assert.equal(result, ['test', 'index.html'].join(path.sep));
assert.equal(result, '/index.html');
});

});

0 comments on commit a876627

Please sign in to comment.