Skip to content

Commit

Permalink
fix: remove code duplication, enable recursive layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
learosema committed Sep 24, 2024
1 parent d8ab70f commit 89cca2a
Showing 1 changed file with 33 additions and 53 deletions.
86 changes: 33 additions & 53 deletions src/sissi.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,79 +92,59 @@ export class Sissi {
}
}

async #handleTemplateFile(content, filename) {
async handleTemplateFile(content, data, filename) {
const parsed = path.parse(filename);
const ext = parsed.ext?.slice(1);
if (! this.config.extensions.has(ext)) {
return { content, data: this.data };
return {
content,
filename: this.config.naming(this.config.dir.output, filename)
};
}

const plugin = this.config.extensions.get(ext);
const { data: matterData, body } = frontmatter(content);
const fileData = Object.assign({}, structuredClone(this.data), matterData);
const processor = await plugin.compile(body, inputFileName);

return { content: template(await processor(fileData))(fileData), data: fileData };
const fileData = Object.assign({}, structuredClone(data), matterData);
const processor = await plugin.compile(body, filename);
let fileContent = template(await processor(fileData))(fileData);
let outputFileName =this.config.naming(this.config.dir.output, filename, plugin?.outputFileExtension);
if (fileData.layout) {
const layoutFile = await this.getFile(this.config.dir.layouts, fileData.layout);
const l = await this.handleTemplateFile(layoutFile.content,
{...fileData, content: fileContent, layout: null}, layoutFile.relPath);
fileContent = l.content;
}
return {content: fileContent, filename: outputFileName};
}

async #getLayout(layout) {
const relLayoutDir = path.normalize(
path.join(this.config.dir.input, this.config.dir.layouts || '_layouts')
async getFile(dir, filename) {
const relPath = path.normalize(
path.join(this.config.dir.input, dir, filename)
);
const filename = path.resolve(relLayoutDir, layout);
const content = await readFile(absLayoutFilePath, 'utf8');
return { content, filename };
const absPath = path.resolve(relPath);
const stats = await stat(absPath);
if (stats.isDirectory()) {
return null;
}
const content = await readFile(absPath, 'utf8');
return { content, relPath, absPath};
}


async processFile(inputFileName, eventEmitter) {
if (! this.data) {
this.data = await readDataDir(this.config);
}
const absInputFileName = path.resolve(this.config.dir.input, inputFileName);
if (inputFileName.startsWith('_') || inputFileName.includes(path.sep + '_')) {
return;
}
const stats = await stat(absInputFileName);
if (stats.isDirectory()) {
const input = await this.getFile('.', inputFileName);
if (! input) {
return;
}
let content = await readFile(absInputFileName, 'utf8');
const parsed = path.parse(inputFileName);
const extension = parsed.ext?.slice(1);

let ext = null;
if (this.config.extensions.has(extension)) {
ext = this.config.extensions.get(extension);
const { data: matterData, body } = frontmatter(content);
content = body;
const fileData = Object.assign({}, structuredClone(this.data), matterData);
const processor = await ext.compile(content, inputFileName);
content = template(await processor(fileData))(fileData);

if (fileData.layout) {
fileData.content = content;
const relLayoutDir = path.normalize(
path.join(this.config.dir.input, this.config.dir.layouts || '_layouts')
);
const absLayoutFilePath = path.resolve(relLayoutDir, fileData.layout);
const layoutExtKey = path.parse(absLayoutFilePath).ext?.slice(1);
let layoutContent = await readFile(absLayoutFilePath, 'utf8');

const layoutExt = layoutExtKey ? this.config.extensions.get(layoutExtKey) : null;
if (layoutExt) {
const processor = await layoutExt.compile(layoutContent, inputFileName);
layoutContent = await processor(fileData);
}

content = template(layoutContent)(fileData);
}


}

let outputFileName =this.config.naming(this.config.dir.output, inputFileName, ext?.outputFileExtension);
console.log(`[write]\t${outputFileName}`);
const tpl = await this.handleTemplateFile(input.content, this.data, inputFileName);

console.log(`[write]\t${tpl.filename}`);
if (eventEmitter) {
eventEmitter.emit('watch-event', {
eventType: 'change',
Expand All @@ -174,8 +154,8 @@ export class Sissi {
if (this.dryMode) {
return;
}
await mkdir(path.parse(outputFileName).dir, {recursive: true});
await writeFile(outputFileName, content, {});
await mkdir(path.parse(tpl.filename).dir, {recursive: true});
await writeFile(tpl.filename, tpl.content, {});
}

/**
Expand Down

0 comments on commit 89cca2a

Please sign in to comment.