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

Provide data about the current Page in the template #10

Merged
merged 2 commits into from
Oct 2, 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
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');
});

});
34 changes: 34 additions & 0 deletions tests/transforms/template-data.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,38 @@ describe('handleTemplateFile function', () => {
});
});

it('should populate data provided by sissi in the template', async () => {
const config = new SissiConfig();
config.addExtension(md);
const vFS = new Map();

const PAGE_TEST = [
'page.url: {{ page.url }}',
'page.filePathStem: {{ page.filePathStem }}',
'page.outputPath: {{ page.outputPath }}',
'page.outputFileExtension: {{ page.outputFileExtension }}',
].join('\n');

const PAGE_TEST_EXPECTED = [
'page.url: /index.html',
'page.filePathStem: /index',
'page.outputPath: public' + path.sep + 'index.html',
'page.outputFileExtension: html',
].join('\n');

vFS.set('index.html', PAGE_TEST);

config.resolve = (...paths) => {
const resource = path.normalize(path.join(...paths));
if (vFS.has(resource)) {
return vFS.get(resource);
}
}

const result = await handleTemplateFile(config, {}, 'index.html');
assert.equal(result.content, PAGE_TEST_EXPECTED);
});



});