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

Add option to configure filter names for url plugin #149

Merged
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
5 changes: 3 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { parse } from "./deps/flags.ts";
import { resolve } from "./deps/path.ts";
import Site from "./core/site.ts";

import url from "./plugins/url.ts";
import url, { Options as UrlOptions } from "./plugins/url.ts";
import json, { Options as JsonOptions } from "./plugins/json.ts";
import markdown, { Options as MarkdownOptions } from "./plugins/markdown.ts";
import modules, { Options as ModulesOptions } from "./plugins/modules.ts";
Expand All @@ -15,6 +15,7 @@ import { merge } from "./core/utils.ts";
import { ServerOptions, SiteOptions, WatcherOptions } from "./core.ts";

interface PluginOptions {
url?: Partial<UrlOptions>;
json?: Partial<JsonOptions>;
markdown?: Partial<MarkdownOptions>;
modules?: Partial<ModulesOptions>;
Expand Down Expand Up @@ -42,7 +43,7 @@ export default function (

return site
.ignore("node_modules")
.use(url())
.use(url(pluginOptions.url))
.use(json(pluginOptions.json))
.use(markdown(pluginOptions.markdown))
.use(modules(pluginOptions.modules))
Expand Down
24 changes: 21 additions & 3 deletions plugins/url.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
import { Helper, Site } from "../core.ts";
import { merge } from "../core/utils.ts";

export interface Options {
/** The url helper name */
names: {
url: string;
htmlUrl: string;
};
}

const defaults: Options = {
names: {
url: 'url',
htmlUrl: 'htmlUrl'
},
};

/**
* A plugin to register the filters "url" and "htmlUrl"
* for normalizing URLs in the templates
*/
export default function () {
export default function (userOptions?: Partial<Options>) {
const options = merge(defaults, userOptions);

return (site: Site) => {
site.filter("url", url as Helper);
site.filter("htmlUrl", htmlUrl as Helper);
site.filter(options.names.url, url as Helper);
site.filter(options.names.htmlUrl, htmlUrl as Helper);

function url(path = "/", absolute = false) {
return typeof path === "string" ? site.url(path, absolute) : path;
Expand Down
17 changes: 17 additions & 0 deletions tests/assets/url/default-filter.tmpl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const title = "Default Filter";
export const url = "/default-filter";

export default function ({}, { url, htmlUrl }) {
return `
<!DOCTYPE html>
<html lang="en">
<head>
<title>Default Filter</title>
</head>
<body>
<a id="url" href="${url?.("/url/", true)}">Url</a>
${htmlUrl?.('<a id="htmlUrl" href="/htmlUrl/">htmlUrl</a>', true)}
</body>
</html>
`;
}
17 changes: 17 additions & 0 deletions tests/assets/url/renamed-filter.tmpl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const title = "Renamed Filter";
export const url = "/renamed-filter";

export default function ({}, { urlify, htmlUrlify }) {
return `
<!DOCTYPE html>
<html lang="en">
<head>
<title>Renamed Filter</title>
</head>
<body>
<a id="urlify" href="${urlify?.("/urlify/", true)}">Urlify</a>
${htmlUrlify?.('<a id="htmlUrlify" href="/htmlUrlify/">htmlUrlify</a>', true)}
</body>
</html>
`;
}
59 changes: 59 additions & 0 deletions tests/url.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { assertStrictEquals as equals } from "../deps/assert.ts";
import { getSite, testPage } from "./utils.ts";

Deno.test("url and htmlUrl update href", async () => {
const site = getSite({
dev: true,
src: "url",
location: new URL("https://example.com/test/"),
});

await site.build();

testPage(site, "/default-filter", (page) => {
equals(
page.document?.querySelector("#url")?.getAttribute("href"),
"https://example.com/test/url/"
);
});

testPage(site, "/default-filter", (page) => {
equals(
page.document?.querySelector("#htmlUrl")?.getAttribute("href"),
"https://example.com/test/htmlUrl/"
);
});

});

Deno.test("configure url and htmlUrl names", async () => {
const site = getSite({
dev: true,
src: "url",
location: new URL("https://example.com/"),
}, {
url: {
names: {
url: 'urlify',
htmlUrl: 'htmlUrlify',
},
},
});

await site.build();

testPage(site, "/renamed-filter", (page) => {
equals(
page.document?.querySelector("#urlify")?.getAttribute("href"),
"https://example.com/urlify/"
);
});

testPage(site, "/renamed-filter", (page) => {
equals(
page.document?.querySelector("#htmlUrlify")?.getAttribute("href"),
"https://example.com/htmlUrlify/"
);
});

});
4 changes: 2 additions & 2 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class TestEmitter implements Emitter {
}

/** Create a new lume site using the "assets" path as cwd */
export function getSite(options: Partial<SiteOptions> = {}): Site {
export function getSite(options: Partial<SiteOptions> = {}, pluginOptions?: any): Site {
options.cwd = cwd;

const site = lume(options, {}, false);
const site = lume(options, pluginOptions, false);
site.emitter = new TestEmitter();

return site;
Expand Down