Skip to content

Commit f28bdd4

Browse files
committed
Allow format-links to control linked formats
1 parent 6d2163f commit f28bdd4

File tree

8 files changed

+74
-20
lines changed

8 files changed

+74
-20
lines changed

src/command/render/render-contexts.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import {
3131
kBibliography,
3232
kCache,
3333
kCss,
34-
kDisplayName,
3534
kEcho,
3635
kEngine,
3736
kExecuteDaemon,
@@ -50,6 +49,7 @@ import {
5049
kOutputExt,
5150
kOutputFile,
5251
kServer,
52+
kTargetFormat,
5353
kTheme,
5454
} from "../../config/constants.ts";
5555
import { resolveLanguageMetadata } from "../../core/language.ts";
@@ -159,7 +159,6 @@ export async function resolveFormatsFromMetadata(
159159

160160
// apply any metadata filter
161161
const defaultFormat = defaultWriterFormat(to);
162-
config[kDisplayName] = config[kDisplayName] || defaultFormat[kDisplayName];
163162
const resolveFormat = defaultFormat.resolveFormat;
164163
if (resolveFormat) {
165164
resolveFormat(config);
@@ -543,6 +542,9 @@ async function resolveFormats(
543542
: {},
544543
userFormat,
545544
);
545+
// Insist that the target format reflect the correct value.
546+
mergedFormats[format].identifier[kTargetFormat] = format;
547+
546548
//deno-lint-ignore no-explicit-any
547549
mergedFormats[format].mergeAdditionalFormats = (...configs: any[]) => {
548550
return mergeFormatMetadata(

src/config/constants.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export const kMetadataFormat = "format";
99

1010
export const kDisplayName = "display-name";
1111
export const kExtensionName = "extension-name";
12+
export const kTargetFormat = "target-format";
13+
export const kIdentifierDefaults = "indentifier";
1214
export const kRenderDefaults = "render";
1315
export const kExecuteDefaults = "execute";
1416
export const kPandocDefaults = "pandoc";
@@ -95,6 +97,12 @@ export const kLinkExternalFilter = "link-external-filter";
9597
export const kQuartoVersion = "quarto-version";
9698
export const kQuartoRequired = "quarto-required";
9799

100+
export const kIdentifierDefaultsKeys = [
101+
kTargetFormat,
102+
kDisplayName,
103+
kExtensionName,
104+
];
105+
98106
export const kExecuteDefaultsKeys = [
99107
kFigWidth,
100108
kFigHeight,

src/config/metadata.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import { mergeArrayCustomizer } from "../core/config.ts";
1616
import { Schema } from "../core/lib/yaml-schema/types.ts";
1717

1818
import {
19-
kDisplayName,
2019
kExecuteDefaults,
2120
kExecuteDefaultsKeys,
2221
kExecuteEnabled,
23-
kExtensionName,
2422
kHeaderIncludes,
23+
kIdentifierDefaults,
24+
kIdentifierDefaultsKeys,
2525
kIncludeAfter,
2626
kIncludeBefore,
2727
kIpynbFilter,
@@ -95,6 +95,7 @@ export function formatFromMetadata(
9595
): Format {
9696
// user format options (allow any b/c this is just untyped yaml)
9797
const typedFormat: Format = {
98+
identifier: {},
9899
render: {},
99100
execute: {},
100101
pandoc: {},
@@ -159,6 +160,7 @@ export function isIncludeMetadata(key: string) {
159160

160161
export function metadataAsFormat(metadata: Metadata): Format {
161162
const typedFormat: Format = {
163+
identifier: {},
162164
render: {},
163165
execute: {},
164166
pandoc: {},
@@ -171,8 +173,7 @@ export function metadataAsFormat(metadata: Metadata): Format {
171173
// allow stuff already sorted into a top level key through unmodified
172174
if (
173175
[
174-
kDisplayName,
175-
kExtensionName,
176+
kIdentifierDefaults,
176177
kRenderDefaults,
177178
kExecuteDefaults,
178179
kPandocDefaults,
@@ -182,9 +183,7 @@ export function metadataAsFormat(metadata: Metadata): Format {
182183
.includes(key)
183184
) {
184185
// special case for 'execute' as boolean
185-
if ([kDisplayName, kExtensionName].includes(key)) {
186-
format[key] = metadata[key];
187-
} else if (typeof (metadata[key]) == "boolean") {
186+
if (typeof (metadata[key]) == "boolean") {
188187
if (key === kExecuteDefaults) {
189188
format[key] = format[key] || {};
190189
format[kExecuteDefaults][kExecuteEnabled] = metadata[key];
@@ -194,7 +193,9 @@ export function metadataAsFormat(metadata: Metadata): Format {
194193
}
195194
} else {
196195
// move the key into the appropriate top level key
197-
if (kRenderDefaultsKeys.includes(key)) {
196+
if (kIdentifierDefaultsKeys.includes(key)) {
197+
format.identifier[key] = metadata[key];
198+
} else if (kRenderDefaultsKeys.includes(key)) {
198199
format.render[key] = metadata[key];
199200
} else if (kExecuteDefaultsKeys.includes(key)) {
200201
format.execute[key] = metadata[key];

src/config/types.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ import {
169169
kStandalone,
170170
kSyntaxDefinitions,
171171
kTableOfContents,
172+
kTargetFormat,
172173
kTblColwidths,
173174
kTemplate,
174175
kTitleBlockAffiliationPlural,
@@ -309,10 +310,15 @@ export interface FormatExtras {
309310
};
310311
}
311312

312-
// pandoc output format
313-
export interface Format {
313+
export interface FormatIdentifier {
314+
[kTargetFormat]?: string;
314315
[kDisplayName]?: string;
315316
[kExtensionName]?: string;
317+
}
318+
319+
// pandoc output format
320+
export interface Format {
321+
identifier: FormatIdentifier;
316322
render: FormatRender;
317323
execute: FormatExecute;
318324
pandoc: FormatPandoc;
@@ -389,7 +395,7 @@ export interface FormatRender {
389395
[kLinkExternalFilter]?: string;
390396
[kSelfContainedMath]?: boolean;
391397
[kFormatResources]?: string[];
392-
[kFormatLinks]?: boolean;
398+
[kFormatLinks]?: boolean | string[];
393399
}
394400

395401
export interface FormatExecute {

src/format/formats-shared.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ export function plaintextFormat(displayName: string, ext: string): Format {
189189

190190
function defaultFormat(displayName: string): Format {
191191
return {
192-
[kDisplayName]: displayName,
192+
identifier: {
193+
[kDisplayName]: displayName,
194+
},
193195
execute: {
194196
[kFigWidth]: 7,
195197
[kFigHeight]: 5,

src/format/formats.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
kFigWidth,
1313
kOutputDivs,
1414
kPageWidth,
15+
kTargetFormat,
1516
kWarning,
1617
} from "../config/constants.ts";
1718

@@ -260,6 +261,9 @@ export function defaultWriterFormat(to: string): Format {
260261
writerFormat.pandoc.to = pandocTo;
261262
}
262263

264+
// Set the originating to
265+
writerFormat.identifier[kTargetFormat] = to;
266+
263267
// return the createFormat
264268
return writerFormat;
265269
}

src/format/html/format-html-bootstrap.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
kRelatedNotebooksTitle,
2525
kSectionDivs,
2626
kSourceNotebookPrefix,
27+
kTargetFormat,
2728
kTocDepth,
2829
kTocLocation,
2930
} from "../../config/constants.ts";
@@ -469,7 +470,29 @@ function processAlternateFormatLinks(
469470

470471
const formatList = doc.createElement("ul");
471472

472-
for (const renderedFormat of options.renderedFormats) {
473+
const formats = Array.isArray(format.render[kFormatLinks])
474+
? format.render[kFormatLinks]
475+
: undefined;
476+
477+
const displayFormats = formats
478+
? options.renderedFormats.filter((renderedFormat) => {
479+
const name = renderedFormat.format.identifier[kTargetFormat];
480+
return !formats || (name && formats.includes(name));
481+
}).sort((a, b) => {
482+
if (
483+
a.format.identifier[kTargetFormat] &&
484+
b.format.identifier[kTargetFormat]
485+
) {
486+
const aIdx = formats.indexOf(a.format.identifier[kTargetFormat]);
487+
const bIdx = formats.indexOf(b.format.identifier[kTargetFormat]);
488+
return aIdx - bIdx;
489+
} else {
490+
return 0;
491+
}
492+
})
493+
: options.renderedFormats;
494+
495+
for (const renderedFormat of displayFormats) {
473496
if (!isHtmlOutput(renderedFormat.format.pandoc, true)) {
474497
const li = doc.createElement("li");
475498

@@ -490,11 +513,11 @@ function processAlternateFormatLinks(
490513
link.appendChild(
491514
doc.createTextNode(
492515
`${
493-
renderedFormat.format[kDisplayName] ||
516+
renderedFormat.format.identifier[kDisplayName] ||
494517
renderedFormat.format.pandoc.to
495518
}${
496-
renderedFormat.format[kExtensionName]
497-
? ` (${renderedFormat.format[kExtensionName]})`
519+
renderedFormat.format.identifier[kExtensionName]
520+
? ` (${renderedFormat.format.identifier[kExtensionName]})`
498521
: ""
499522
}`,
500523
),

src/resources/schema/document-links.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@
2828
- name: format-links
2929
tags:
3030
formats: [$html-doc]
31-
schema: boolean
31+
schema:
32+
anyOf:
33+
- boolean
34+
- arrayOf: string
3235
description:
33-
short: "Controls whether links to other rendered formats are displayed in HTML output"
36+
short: "Controls whether links to other rendered formats are displayed in HTML output."
37+
long: |
38+
Controls whether links to other rendered formats are displayed in HTML output.
39+
40+
Pass `false` to disable the display of format lengths or pass a list of format names for which you'd
41+
like links to be shown.

0 commit comments

Comments
 (0)