Skip to content

Commit

Permalink
Add djot support
Browse files Browse the repository at this point in the history
  • Loading branch information
iacore committed Apr 8, 2024
1 parent bec18f8 commit d23b482
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions deps/djot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "npm:@djot/[email protected]";
95 changes: 95 additions & 0 deletions plugins/djot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import {
type HTMLRenderOptions,
parse,
type ParseOptions,
renderHTML,
} from "../deps/djot.ts";
import loader from "../core/loaders/text.ts";
import { merge } from "../core/utils/object.ts";

import type Site from "../core/site.ts";
import type { Engine } from "../core/renderer.ts";

export interface Options {
/** The list of extensions this plugin applies to */
extensions?: string[];

/** Options passed to djot library */
parseOptions?: ParseOptions;

/** Options passed to djot library */
renderOptions?: HTMLRenderOptions;
}

// Default options
export const defaults: Options = {
extensions: [".dj", ".djot"],
};

/** Template engine to render Markdown files */
export class DjotEngine implements Engine {
parseOptions: ParseOptions;
renderOptions: HTMLRenderOptions;

constructor() {
}

deleteCache() {}

render(
content: string,
data?: Record<string, unknown>,
filename?: string,
): string {
return this.renderComponent(content, data, filename);
}

renderComponent(
content: unknown,
data?: Record<string, unknown>,
filename?: string,
): string {
if (typeof content !== "string") {
content = String(content);
}
const doc = parse(content, this.parseOptions);
return renderHTML(doc, this.renderOptions);
}

addHelper() {}
}

function render(doc) {
}

/** Register the plugin to support Djot */
export default function (userOptions?: Options) {
const options = merge(defaults, userOptions);

return function (site: Site) {
// Load the pages
site.loadPages(options.extensions, {
loader,
engine: new DjotEngine(),
});

// Register the md filter
site.filter("dj", filter);

function filter(string: string, inline = false): string {
const content = string?.toString() || "";
const doc = parse(content, {});
return renderHTML(doc, {});
}
};
}

/** Extends Helpers interface */
declare global {
namespace Lume {
export interface Helpers {
/** @see https://lume.land/plugins/markdown/ */
dj: (string: string, inline?: boolean) => string;
}
}
}

0 comments on commit d23b482

Please sign in to comment.