Skip to content

Commit ad8ed05

Browse files
committed
enable use of custom HTML formats for websites
1 parent 072c235 commit ad8ed05

File tree

8 files changed

+28
-35
lines changed

8 files changed

+28
-35
lines changed

news/changelog-1.2.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
- Properly allow `twitter-card` and `open-graph` to override the page description.
3131
- Don't discover resources within a site or book output directory
32+
- Enable use of custom HTML formats for websites
3233

3334
## Publishing
3435

src/project/project-context.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ export async function projectContext(
105105
path: string,
106106
flags?: RenderFlags,
107107
force = false,
108-
forceHtml = false,
109108
): Promise<ProjectContext | undefined> {
110109
let dir = Deno.realPathSync(
111110
Deno.statSync(path).isDirectory ? path : dirname(path),
@@ -225,7 +224,6 @@ export async function projectContext(
225224
projectConfig = await type.config(
226225
dir,
227226
projectConfig,
228-
forceHtml,
229227
flags,
230228
);
231229
}

src/project/project-index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import { readYamlFromString } from "../core/yaml.ts";
3131
import { formatKeys } from "../config/metadata.ts";
3232
import {
3333
formatsPreferHtml,
34-
normalizeWebsiteFormat,
34+
websiteFormatPreferHtml,
3535
} from "./types/website/website-config.ts";
3636
import { kDefaultProjectFileContents } from "./types/project-default.ts";
3737
import { formatOutputFile } from "../core/render.ts";
@@ -113,7 +113,9 @@ export function readInputTargetIndex(
113113
if (index) {
114114
// normalize html to first if its included in the formats
115115
if (Object.keys(index.formats).includes("html")) {
116-
index.formats = normalizeWebsiteFormat(index.formats, true) as Record<
116+
// note that the cast it okay here b/c we know that index.formats
117+
// includes only full format objects
118+
index.formats = websiteFormatPreferHtml(index.formats) as Record<
117119
string,
118120
Format
119121
>;

src/project/serve/serve.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export async function serveProject(
116116
if (target === ".") {
117117
target = Deno.cwd();
118118
}
119-
project = await projectContext(target, flags, false, true);
119+
project = await projectContext(target, flags, false);
120120
if (!project || !project?.config) {
121121
throw new Error(`${target} is not a website or book project`);
122122
}

src/project/serve/watch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export function watchProject(
5454
): Promise<ProjectWatcher> {
5555
// helper to refresh project config
5656
const refreshProjectConfig = async () => {
57-
project = (await projectContext(project.dir, flags, false, true))!;
57+
project = (await projectContext(project.dir, flags, false))!;
5858
};
5959

6060
// proj dir

src/project/types/book/book-config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ export const kBookItemPart = "part";
104104
export async function bookProjectConfig(
105105
projectDir: string,
106106
config: ProjectConfig,
107-
forceHtml: boolean,
108107
flags?: RenderFlags,
109108
) {
110109
// ensure we have a site
@@ -227,7 +226,7 @@ export async function bookProjectConfig(
227226
}
228227

229228
// return config (inherit website config behavior)
230-
return websiteProjectConfig(projectDir, config, forceHtml);
229+
return websiteProjectConfig(projectDir, config);
231230
}
232231

233232
function siteRepoUrl(site: Metadata) {

src/project/types/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export interface ProjectType {
2626
config?: (
2727
projectDir: string,
2828
config: ProjectConfig,
29-
forceHtml: boolean,
3029
flags?: RenderFlags,
3130
) => Promise<ProjectConfig>;
3231
libDir?: string;

src/project/types/website/website-config.ts

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -246,41 +246,35 @@ export function websiteConfigActions(
246246
}
247247
}
248248

249-
export function normalizeWebsiteFormat(
249+
// normalize html to first if its included in the formats
250+
export function websiteFormatPreferHtml(
250251
format: string | Record<string, unknown> | undefined,
251-
forceHtml: boolean,
252-
): string | Record<string, unknown> {
252+
): Record<string, unknown> {
253253
if (format !== undefined) {
254254
if (typeof (format) === "string") {
255-
if (!isHtmlOutput(format, true) && forceHtml) {
256-
return {
257-
html: "default",
258-
[format]: "default",
259-
};
260-
} else {
261-
return format;
262-
}
255+
return {
256+
[format]: "default",
257+
};
263258
} else {
264259
const formats = Object.keys(format);
265-
const orderedFormats = {} as Record<string, unknown>;
266-
if (forceHtml) {
267-
const htmlFormatPos = formats.findIndex((format) =>
268-
isHtmlOutput(format, true)
269-
);
270-
if (htmlFormatPos !== -1) {
271-
const htmlFormatName = formats.splice(htmlFormatPos, 1)[0];
272-
orderedFormats[htmlFormatName] = format[htmlFormatName];
273-
} else {
274-
orderedFormats["html"] = "default";
275-
}
260+
const orderedFormats = {} as Record<string, Format>;
261+
262+
const htmlFormatPos = formats.findIndex((format) =>
263+
isHtmlOutput(format, true)
264+
);
265+
if (htmlFormatPos !== -1) {
266+
const htmlFormatName = formats.splice(htmlFormatPos, 1)[0];
267+
orderedFormats[htmlFormatName] = format[htmlFormatName] as Format;
276268
}
277269
for (const formatName of formats) {
278-
orderedFormats[formatName] = format[formatName];
270+
orderedFormats[formatName] = format[formatName] as Format;
279271
}
280272
return orderedFormats;
281273
}
282274
} else {
283-
return "html";
275+
return {
276+
html: "default",
277+
};
284278
}
285279
}
286280

@@ -305,15 +299,15 @@ export function formatsPreferHtml(formats: Record<string, unknown>) {
305299
export function websiteProjectConfig(
306300
_projectDir: string,
307301
config: ProjectConfig,
308-
forceHtml: boolean,
309302
flags?: RenderFlags,
310303
): Promise<ProjectConfig> {
311304
config = ld.cloneDeep(config);
312305
const format = config[kMetadataFormat] as
313306
| string
314307
| Record<string, unknown>
315308
| undefined;
316-
config[kMetadataFormat] = normalizeWebsiteFormat(format, forceHtml);
309+
310+
config[kMetadataFormat] = websiteFormatPreferHtml(format);
317311

318312
// Resolve elements to be sure they're arrays, they will be resolve later
319313
const ensureArray = (val: unknown) => {

0 commit comments

Comments
 (0)