Skip to content

Commit

Permalink
Improve flexibilty of format-links
Browse files Browse the repository at this point in the history
Users may now write:

```
format-links:
  - html
  - format: typst
    text: PDF
    icon: pdf
  - title: Other Link
     href: https://www.charlesteague.com
     icon: thumbs-up
```

Fixes #7459
  • Loading branch information
dragonstyle committed Nov 30, 2023
1 parent 2c61613 commit 6b62246
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 57 deletions.
7 changes: 7 additions & 0 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,13 @@ export interface FormatLink {
attr?: Record<string, string>;
}

export interface FormatAliasLink {
icon?: string;
text: string;
format: string;
attr?: Record<string, string>;
}

export interface OtherLink {
icon?: string;
text: string;
Expand Down
57 changes: 39 additions & 18 deletions src/format/html/format-html-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
kFormatLinks,
kTargetFormat,
} from "../../config/constants.ts";
import { Format, FormatLink } from "../../config/types.ts";
import { Format, FormatAliasLink, FormatLink } from "../../config/types.ts";

import { RenderedFormat } from "../../command/render/types.ts";
import {
Expand All @@ -23,6 +23,7 @@ import {
isMarkdownOutput,
isPdfOutput,
isPresentationOutput,
isTypstOutput,
} from "../../config/format.ts";
import { basename } from "path/mod.ts";
import { extname } from "path/mod.ts";
Expand All @@ -43,14 +44,14 @@ export function otherFormatLinks(
) {
const normalizedFormatLinks = (
unnormalizedLinks: unknown,
): Array<string | FormatLink> | undefined => {
if (typeof (unnormalizedLinks) === "boolean") {
): Array<string | FormatLink | FormatAliasLink> | undefined => {
if (typeof unnormalizedLinks === "boolean") {
return undefined;
} else if (unnormalizedLinks !== undefined) {
const linksArr: unknown[] = Array.isArray(unnormalizedLinks)
? unnormalizedLinks
: [unnormalizedLinks];
return linksArr as Array<string | FormatLink>;
return linksArr as Array<string | FormatLink | FormatAliasLink>;
} else {
return undefined;
}
Expand All @@ -74,19 +75,22 @@ export function otherFormatLinks(
export function alternateLinks(
input: string,
formats: RenderedFormat[],
userLinks?: Array<string | FormatLink>,
userLinks?: Array<string | FormatLink | FormatAliasLink>,
): AlternateLink[] {
const alternateLinks: AlternateLink[] = [];

const alternateLinkForFormat = (
renderedFormat: RenderedFormat,
order: number,
title?: string,
icon?: string,
) => {
const relPath = isAbsolute(renderedFormat.path)
? relative(dirname(input), renderedFormat.path)
: renderedFormat.path;
return {
title: `${
title ||
renderedFormat.format.identifier[kDisplayName] ||
renderedFormat.format.pandoc.to
}${
Expand All @@ -95,7 +99,7 @@ export function alternateLinks(
: ""
}`,
href: relPath,
icon: fileBsIconName(renderedFormat.format),
icon: icon || fileBsIconName(renderedFormat.format),
order,
dlAttrValue: fileDownloadAttr(
renderedFormat.format,
Expand All @@ -106,7 +110,7 @@ export function alternateLinks(

let count = 1;
for (const userLink of userLinks || []) {
if (typeof (userLink) === "string") {
if (typeof userLink === "string") {
// We need to filter formats, otherwise, we'll deal
// with them below
const renderedFormat = formats.find((f) =>
Expand All @@ -117,22 +121,37 @@ export function alternateLinks(
alternateLinks.push(alternateLinkForFormat(renderedFormat, count));
}
} else {
// This an explicit link
const alternate = {
title: userLink.text,
href: userLink.href,
icon: userLink.icon || fileBsIconForExt(userLink.href),
dlAttrValue: "",
order: userLink.order || count,
attr: userLink.attr,
};
alternateLinks.push(alternate);
const linkObj = userLink as FormatLink | FormatAliasLink;
if ("format" in linkObj) {
const thatLink = userLink as FormatAliasLink;
const rf = formats.find((f) =>
f.format.identifier[kTargetFormat] === thatLink.format
);
if (rf) {
// Just push through
alternateLinks.push(
alternateLinkForFormat(rf, count, thatLink.text, thatLink.icon),
);
}
} else {
// This an explicit link
const thisLink = userLink as FormatLink;
const alternate = {
title: thisLink.text,
href: thisLink.href,
icon: thisLink.icon || fileBsIconForExt(thisLink.href),
dlAttrValue: "",
order: thisLink.order || count,
attr: thisLink.attr,
};
alternateLinks.push(alternate);
}
}
count++;
}

const userLinksHasFormat = userLinks &&
userLinks.some((link) => typeof (link) === "string");
userLinks.some((link) => typeof link === "string");
if (!userLinksHasFormat) {
formats.forEach((renderedFormat) => {
const baseFormat = renderedFormat.format.identifier["base-format"];
Expand All @@ -152,6 +171,8 @@ const fileBsIconName = (format: Format) => {
return "file-word";
} else if (isPdfOutput(format.pandoc)) {
return "file-pdf";
} else if (isTypstOutput(format.pandoc)) {
return "file-pdf";
} else if (isIpynbOutput(format.pandoc)) {
return "journal-code";
} else if (isMarkdownOutput(format)) {
Expand Down
66 changes: 54 additions & 12 deletions src/resources/editor/tools/vs-code.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15023,20 +15023,50 @@ var require_yaml_intelligence_resources = __commonJS({
properties: {
text: {
string: {
description: "The title for this alternative link."
description: "The title for the link."
}
},
href: {
string: {
description: "The href for tihs alternative link."
description: "The href for the link."
}
},
icon: {
string: {
description: "The icon for the link."
}
}
},
required: [
"title",
"text",
"href"
]
}
},
{
object: {
properties: {
format: {
string: {
description: "The format that this link represents."
}
},
text: {
string: {
description: "The title for this link."
}
},
icon: {
string: {
description: "The icon for this link."
}
}
},
required: [
"text",
"format"
]
}
}
]
}
Expand Down Expand Up @@ -21181,10 +21211,22 @@ var require_yaml_intelligence_resources = __commonJS({
short: "Controls whether links to other rendered formats are displayed in\nHTML output.",
long: "Controls whether links to other rendered formats are displayed in\nHTML output.\nPass <code>false</code> to disable the display of format lengths or\npass a list of format names for which you\u2019d like links to be shown."
},
"The title for this alternative link.",
"The href for tihs alternative link.",
"The title for this alternative link.",
"The href for tihs alternative link.",
"The title for the link.",
"The href for the link.",
"The icon for the link.",
"The format that this link represents.",
"The title for this link.",
"The icon for this link.",
"The rel for this link.",
"The target for this link.",
"The title for the link.",
"The href for the link.",
"The icon for the link.",
"The format that this link represents.",
"The title for this link.",
"The icon for this link.",
"The rel for this link.",
"The target for this link.",
{
short: "Controls the display of links to notebooks that provided embedded\ncontent or are created from documents.",
long: "Controls the display of links to notebooks that provided embedded\ncontent or are created from documents.\nSpecify <code>false</code> to disable linking to source Notebooks.\nSpecify <code>inline</code> to show links to source notebooks beneath\nthe content they provide. Specify <code>global</code> to show a set of\nglobal links to source notebooks."
Expand Down Expand Up @@ -21446,6 +21488,7 @@ var require_yaml_intelligence_resources = __commonJS({
"Bounds for largest possible scale to apply to content",
"Vertical centering of slides",
"Disables the default reveal.js slide layout (scaling and\ncentering)",
"Sets the maximum height for source code blocks that appear in the\npresentation.",
{
short: "Open links in an iframe preview overlay (<code>true</code>,\n<code>false</code>, or <code>auto</code>)",
long: "Open links in an iframe preview overlay."
Expand Down Expand Up @@ -22267,8 +22310,7 @@ var require_yaml_intelligence_resources = __commonJS({
},
"Disambiguating year suffix in author-date styles (e.g.&nbsp;\u201Ca\u201D in \u201CDoe,\n1999a\u201D).",
"Manuscript configuration",
"internal-schema-hack",
"Sets the maximum height for source code blocks that appear in the\npresentation."
"internal-schema-hack"
],
"schema/external-schemas.yml": [
{
Expand Down Expand Up @@ -22492,12 +22534,12 @@ var require_yaml_intelligence_resources = __commonJS({
mermaid: "%%"
},
"handlers/mermaid/schema.yml": {
_internalId: 178765,
_internalId: 179968,
type: "object",
description: "be an object",
properties: {
"mermaid-format": {
_internalId: 178757,
_internalId: 179960,
type: "enum",
enum: [
"png",
Expand All @@ -22513,7 +22555,7 @@ var require_yaml_intelligence_resources = __commonJS({
exhaustiveCompletions: true
},
theme: {
_internalId: 178764,
_internalId: 179967,
type: "anyOf",
anyOf: [
{
Expand Down
66 changes: 54 additions & 12 deletions src/resources/editor/tools/yaml/web-worker.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6b62246

Please sign in to comment.