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
44 changes: 44 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: CI

on:
pull_request:
branches: [main]
push:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20, 22]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: npm
- run: npm ci
- run: npm test

typecheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npx tsc --noEmit

format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npx prettier --config .prettierrc 'src/**/*.ts' 'test/**/*.ts' --check
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"engines": {
"npm": ">=8.0.0",
"node": ">=18.0.0"
"node": ">=20.0.0"
},
"bin": "lib/cli.js",
"type": "module",
Expand Down
32 changes: 23 additions & 9 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const slugify = (_slugify as any).default as (str: string) => string;
import Data from './helpers/Data.ts';
import { mergeTree } from './helpers/dom-diff.ts';
import { ASTNode } from './types.ts';
import VariationMap, { remapData, remapASTReferences } from './helpers/variation-map.ts';

export interface CollectionConfig {
name: string;
Expand Down Expand Up @@ -65,6 +66,8 @@ export default class Collection {
}

async build(): Promise<CollectionResponse> {
const variationMap = new VariationMap();

const documents = await Promise.all(
this.files.map(async (file) => {
const html = await file.read();
Expand All @@ -76,7 +79,8 @@ export default class Collection {
content: this.content
},
processorConfig: this.processorConfig || {},
logger: this.logger
logger: this.logger,
variationMap
});
})
);
Expand Down Expand Up @@ -105,8 +109,7 @@ export default class Collection {
await this.logger.writeLog('layout.json', JSON.stringify(current.layout, null, '\t'));
for (let i = 2; i < documents.length; i++) {
await this.logger.rotateLog();
try
{
try {
const next = baseDoc.diff(documents[i]);
await this.logger.writeLog(
`${slugify(documents[i].pathname)}.json`,
Expand All @@ -117,7 +120,6 @@ export default class Collection {

const currentPreMergeData = structuredClone(current.base.data.data);
const nextPreMergeData = structuredClone(next.base.data.data);
// Merge the next and current bases
const layout = mergeTree(
current.base.data,
next.base.data,
Expand All @@ -130,12 +132,14 @@ export default class Collection {
for (let i = 0; i < current.pages.length; i++) {
const page = current.pages[i];
const nextData = new Data([], structuredClone(nextPreMergeData));
nextData.variationMap = variationMap;
mergeTree(page.data, nextData, current.layout, next.layout, [], this.logger);
}

for (let i = 0; i < next.pages.length; i++) {
const page = next.pages[i];
const currentData = new Data([], structuredClone(currentPreMergeData));
currentData.variationMap = variationMap;
mergeTree(page.data, currentData, next.layout, current.layout, [], this.logger);
}

Expand All @@ -146,18 +150,28 @@ export default class Collection {
pages: [...current.pages, ...next.pages],
layout
};
}
catch(err){
console.error("Error hit exporting log")
} catch (err) {
console.error('Error hit exporting log');
await this.logger.rotateLog();
throw err;
}
}
await this.logger.rotateLog();

const displayNames = variationMap.generateDisplayNames();
const remappedLayout = remapASTReferences(current.layout, displayNames);
const remappedPages = [
current.base.toJSON(),
...current.pages.map((page) => page.toJSON())
].map((page) => ({
...page,
data: remapData(page.data, displayNames),
content: remapASTReferences(page.content, displayNames)
}));

return {
pages: [current.base.toJSON(), ...current.pages.map((page) => page.toJSON())],
layout: current.layout
pages: remappedPages,
layout: remappedLayout
};
}
}
6 changes: 6 additions & 0 deletions src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import htmlToAST, { HtmlProcessorConfig, PageContentsConfig } from './helpers/ht
import { ASTNode } from './types.ts';
import Data from './helpers/Data.ts';
import { Logger } from './logger.ts';
import VariationMap from './helpers/variation-map.ts';

export interface ASTTree {
base: Page;
Expand All @@ -22,6 +23,7 @@ export interface DocumentOptions {
config: DocumentConfig;
processorConfig: HtmlProcessorConfig;
logger: Logger;
variationMap?: VariationMap;
}

export default class Document {
Expand All @@ -30,10 +32,12 @@ export default class Document {
data: Data;
layout: ASTNode[];
contents: ASTNode[];
variationMap?: VariationMap;

constructor(options: DocumentOptions) {
this.logger = options.logger;
this.pathname = options.pathname;
this.variationMap = options.variationMap;
const { layout, contents } = htmlToAST(
options.content,
options.config,
Expand All @@ -42,10 +46,12 @@ export default class Document {
this.layout = layout;
this.contents = contents;
this.data = new Data([], {});
this.data.variationMap = this.variationMap;
}

diff(other: Document): ASTTree {
this.data = new Data([], {});
this.data.variationMap = this.variationMap;

this.logger.log(`Comparing ${this.pathname} and ${other.pathname}`);
const tree = mergeTree(this.data, other.data, this.layout, other.layout, [], this.logger);
Expand Down
38 changes: 21 additions & 17 deletions src/export-engines/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export default class HtmlExportEngine {
throw new Error(`${node.type} render: not yet implemented`);
}

renderDoctype(doctype: ASTDoctypeNode, variableScope: string): string {
renderDoctype(doctype: ASTDoctypeNode, _variableScope: string): string {
return `<!DOCTYPE ${doctype.value}>`;
}

Expand All @@ -119,15 +119,18 @@ export default class HtmlExportEngine {
.join(' ')}`;
}

renderAttribute(attr: ASTStaticAttribute, variableScope: string): string {
renderAttribute(attr: ASTStaticAttribute, _variableScope: string): string {
return [attr.name, `"${attr.value}"`].join('=');
}

renderVariableAttribute(_attr: ASTVariableAttribute | ASTConditionalAttribute, variableScope: string): string {
renderVariableAttribute(
_attr: ASTVariableAttribute | ASTConditionalAttribute,
_variableScope: string
): string {
throw new Error('Not yet implemented');
}

renderConditionalAttribute(_attr: ASTConditionalAttribute, variableScope: string): string {
renderConditionalAttribute(_attr: ASTConditionalAttribute, _variableScope: string): string {
throw new Error('Not yet implemented');
}

Expand All @@ -138,53 +141,54 @@ export default class HtmlExportEngine {
name = name.substring(5);
}

// Elements that don't need closing tags
if(name === "meta" || name === "link" || name === "img"){
// Elements that don't need closing tags
if (name === 'meta' || name === 'link' || name === 'img') {
return `<${name}${this.renderAttributes(element.attrs, variableScope)}>${this.renderAST(element.children, variableScope)}`;
}
// // TODO: Fix style tags
// // TODO: Fix style tags
// if(name === "style"){
// return `<!-- Style was here -->`;
// }

// TODO: Fix scripts
if(name === "script"){
if (name === 'script') {
return `<!-- Script was here -->`;
}
return `<${name}${this.renderAttributes(element.attrs, variableScope)}>${this.renderAST(
element.children, variableScope
element.children,
variableScope
)}</${name}>`;
}

renderText(text: ASTTextNode, variableScope: string): string {
renderText(text: ASTTextNode, _variableScope: string): string {
return text.value;
}

renderComment(text: ASTCommentNode, variableScope: string): string {
renderComment(text: ASTCommentNode, _variableScope: string): string {
return `<!-- ${text.value} -->`;
}

renderVariable(_node: ASTVariableNode, variableScope: string): string {
renderVariable(_node: ASTVariableNode, _variableScope: string): string {
throw new Error('Variable render not yet implemented');
}

renderMarkdownVariable(_node: ASTMarkdownNode, variableScope: string): string {
renderMarkdownVariable(_node: ASTMarkdownNode, _variableScope: string): string {
throw new Error('Markdown render not yet implemented');
}

renderInlineMarkdownVariable(_node: ASTInlineMarkdownNode, variableScope: string): string {
renderInlineMarkdownVariable(_node: ASTInlineMarkdownNode, _variableScope: string): string {
throw new Error('Inline markdown render not yet implemented');
}

renderConditional(_node: ASTConditionalNode, variableScope: string): string {
renderConditional(_node: ASTConditionalNode, _variableScope: string): string {
throw new Error('Conditional render not yet implemented');
}

renderLoop(_node: ASTLoopNode, variableScope: string): string {
renderLoop(_node: ASTLoopNode, _variableScope: string): string {
throw new Error('Loop render not yet implemented');
}

renderContent(_node: ASTContentNode, variableScope: string): string {
renderContent(_node: ASTContentNode, _variableScope: string): string {
throw new Error('Content render not yet implemented');
}
}
46 changes: 31 additions & 15 deletions src/export-engines/hugo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,20 @@ function renderFrontMatter(data: Record<string, any>) {
}

/**
*
* @param node
*
* @param node
* @returns a string that is hugo formatted
*/
function formatParam(node: ASTVariableNode | ASTMarkdownNode | ASTInlineMarkdownNode | ASTConditionalNode | ASTLoopNode | ASTVariableAttribute | ASTConditionalAttribute){
function formatParam(
node:
| ASTVariableNode
| ASTMarkdownNode
| ASTInlineMarkdownNode
| ASTConditionalNode
| ASTLoopNode
| ASTVariableAttribute
| ASTConditionalAttribute
) {
return node.reference.join('.').replaceAll(/([\-\:\s\@])+/g, '_');
}

Expand All @@ -50,19 +59,19 @@ export default class HugoExportEngine extends HtmlExportEngine {

exportLayout(
layout: ASTNode[],
collection: CollectionResponse,
_collection: CollectionResponse,
collectionKey: string
): FileExport {
return {
// TODO: this logic is currently fixed to use a default layout (list.html)
pathname: `layouts/${collectionKey}/list.html`,
contents: this.renderAST(layout, ".Params.")
contents: this.renderAST(layout, '.Params.')
};
}

exportCollectionItem(
item: Record<string, any>,
collection: CollectionResponse,
_collection: CollectionResponse,
collectionKey: string
): FileExport {
const folder = collectionKey !== 'pages' ? `${collectionKey}/` : '';
Expand All @@ -73,10 +82,13 @@ export default class HugoExportEngine extends HtmlExportEngine {
};
return {
pathname: `content/${folder}${item.pathname.replace('index', '_index')}`,
contents: [renderFrontMatter(frontMatter), this.renderAST(item.content, ".Params.")].join('\n')
contents: [
renderFrontMatter(frontMatter),
this.renderAST(item.content, '.Params.')
].join('\n')
};
}

renderVariable(node: ASTVariableNode, variableScope: string): string {
return `{{ ${variableScope}${formatParam(node)} }}`;
}
Expand All @@ -89,29 +101,33 @@ export default class HugoExportEngine extends HtmlExportEngine {
// return `{{ .Params.${formatParam(node)} | fake_inline_markdownify_filter }}`;
// TODO: fix fake_inline_markdownify_filter
return `{{ ${variableScope}${formatParam(node)} | markdownify }}`;

}

renderConditional(node: ASTConditionalNode, variableScope: string): string {
return `{{ with ${variableScope}${formatParam(node)} }} ${this.renderASTNode(
node.template, "."
node.template,
'.'
)}{{ end }}`;
}

renderLoop(node: ASTLoopNode, variableScope: string): string {
return `{{ range ${variableScope}${formatParam(node)} }}${this.renderASTNode(
node.template, "."
node.template,
'.'
)}{{ end }}`;
}

renderContent(_node: ASTContentNode, variableScope: string): string {
return `{{ .Content }}`;
renderContent(_node: ASTContentNode, _variableScope: string): string {
return `{{ .Content }}`;
}

renderVariableAttribute(attr: ASTVariableAttribute | ASTConditionalAttribute, variableScope: string): string {
renderVariableAttribute(
attr: ASTVariableAttribute | ASTConditionalAttribute,
variableScope: string
): string {
return [attr.name, `"{{ ${variableScope}${formatParam(attr)} }}"`].join('=');
}

renderConditionalAttribute(attr: ASTConditionalAttribute, variableScope: string): string {
return `{{ with ${variableScope}${formatParam(attr)} }}{{ . }}{{ end }}`;
}
Expand Down
Loading