Skip to content

Commit 39eab2f

Browse files
authored
feat: add target and rel to website listing links (#15269)
Listings and listing items now accept `target` and `rel`, with the same meaning as on navbar and sidebar items. A listing-level value applies to all its items, and an item value wins. A new-tab target without `rel` defaults to `noopener`. - Default, grid and table listings build link attributes from one helper, `listing.utilities.targetAttrs(item)`. Feeds and custom templates ignore both options. - With `table-hover`, a row click on an item with a target clicks the row's link, so the browser applies its `target` and `rel`. - `encodeAttributeValue` escaped only the first match of each character and double-escaped `&`, which broke `image-alt` values containing quotes. It now escapes every character once, and table links escape the item path too. - `link-external-newwindow` no longer overrides a `target` set explicitly on a link. - Every link that opens a new tab gets the "(opens in a new tab)" description, not only links the option opened. See #12276 See #9469 See quarto-dev/quarto-web#2338
1 parent 664486a commit 39eab2f

31 files changed

Lines changed: 625 additions & 38 deletions

‎news/changelog-1.12.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
All changes included in 1.12:
22

3+
## Accessibility
4+
5+
- ([#15269](https://github.com/quarto-dev/quarto-cli/pull/15269)): Announce every link that opens a new tab to screen readers, not only links opened by `link-external-newwindow`. (author: @mcanouil)
6+
37
## Formats
48

9+
### `html`
10+
11+
- ([#15269](https://github.com/quarto-dev/quarto-cli/pull/15269)): Fix `link-external-newwindow` overriding a `target` set explicitly on a link. (author: @mcanouil)
12+
513
### `pdf`
614

715
- ([#15251](https://github.com/quarto-dev/quarto-cli/pull/15251)): Fix algorithm blocks (`#alg-`) being set in italics. Their body is now upright, like definitions, examples and exercises. (author: @mcanouil)
@@ -11,6 +19,13 @@ All changes included in 1.12:
1119
- ([#15168](https://github.com/quarto-dev/quarto-cli/issues/15168)): Fix definitions, examples and exercises being set in italics in Typst output with the default `theorem-appearance: simple`. Their body is now upright, as in LaTeX output, while theorems, lemmas, corollaries, propositions and conjectures keep an italic body. (author: @mcanouil)
1220
- ([#15251](https://github.com/quarto-dev/quarto-cli/pull/15251)): Fix algorithm blocks (`#alg-`) being set in italics with `theorem-appearance: simple` and using the colours of theorems with `theorem-appearance: fancy`. They now look like definitions, examples and exercises. (author: @mcanouil)
1321

22+
## Projects
23+
24+
### `website`
25+
26+
- ([#15269](https://github.com/quarto-dev/quarto-cli/pull/15269)): Fix listing `image-alt` values containing quotes or `&` rendering broken or double-escaped alt text. (author: @mcanouil)
27+
- ([#15269](https://github.com/quarto-dev/quarto-cli/pull/15269)): Add `target` and `rel` options to listings and listing items, with the listing value as the default for its items. (author: @mcanouil)
28+
1429
## Commands
1530

1631
### `quarto preview`

‎src/core/html.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ const kTagBrackets: Record<string, string> = {
4040
};
4141

4242
const kAttrReplacements: Record<string, string> = {
43+
"&": "&amp;",
4344
'"': "&quot;",
4445
"'": "&#039;",
4546
...kTagBrackets,
46-
"&": "&amp;",
4747
};
4848
export function encodeAttributeValue(value: unknown) {
4949
if (typeof (value) === "string") {
5050
let result: string = value as string;
5151
Object.keys(kAttrReplacements).forEach((key) => {
52-
result = result.replace(key, kAttrReplacements[key]);
52+
result = result.replaceAll(key, kAttrReplacements[key]);
5353
});
5454
return result;
5555
} else {

‎src/project/types/website/listing/website-listing-read.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ import {
1515
relative,
1616
} from "../../../../deno_ral/path.ts";
1717
import { cloneDeep, orderBy, uniq } from "../../../../core/lodash.ts";
18-
import {
19-
existsSync,
20-
safeExistsFileSync,
21-
} from "../../../../deno_ral/fs.ts";
18+
import { existsSync, safeExistsFileSync } from "../../../../deno_ral/fs.ts";
2219

2320
import { Format, Metadata } from "../../../../config/types.ts";
2421
import {
@@ -72,11 +69,13 @@ import {
7269
kListing,
7370
kMaxDescLength,
7471
kPageSize,
72+
kRel,
7573
kSortAsc,
7674
kSortDesc,
7775
kSortUi,
7876
kTableHover,
7977
kTableStriped,
78+
kTarget,
8079
Listing,
8180
ListingDehydrated,
8281
ListingDescriptor,
@@ -600,7 +599,7 @@ function hydrateListing(
600599
// If the items have come from metadata, we should just show
601600
// all the columns in the table. Otherwise, we should use the
602601
// document default columns
603-
const undisplayable = ["path"];
602+
const undisplayable = ["path", kTarget, kRel];
604603
return itemFields.filter((field) => {
605604
return !undisplayable.includes(field);
606605
});

‎src/project/types/website/listing/website-listing-shared.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ export const kDefaultMaxDescLength = 175;
9090
export const kTableStriped = "table-striped";
9191
export const kTableHover = "table-hover";
9292

93+
// Link attributes
94+
export const kTarget = "target";
95+
export const kRel = "rel";
96+
9397
// Items to include
9498
export const kInclude = "include";
9599
export const kExclude = "exclude";
@@ -173,6 +177,8 @@ export interface Listing extends ListingDehydrated {
173177
[kFilterUi]: boolean;
174178
[kSortUi]: boolean;
175179
[kImagePlaceholder]?: string;
180+
[kTarget]?: string;
181+
[kRel]?: string;
176182

177183
sort?: ListingSort[];
178184
template?: string;
@@ -216,6 +222,8 @@ export interface ListingItem extends Record<string, unknown> {
216222
image?: string;
217223
[kImageAlt]?: string;
218224
[kImageLazyLoading]?: boolean;
225+
[kTarget]?: string;
226+
[kRel]?: string;
219227
path?: string;
220228
filename?: string;
221229
[kFieldFileModified]?: Date;

‎src/project/types/website/listing/website-listing-template.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ import {
3131
kMaxDescLength,
3232
kMaxItems,
3333
kPageSize,
34+
kRel,
3435
kSortAsc,
3536
kSortDesc,
37+
kTarget,
3638
Listing,
3739
ListingItem,
3840
ListingSort,
@@ -311,6 +313,8 @@ export interface ReshapedListing extends Listing {
311313
utilities: Record<string, unknown>;
312314
}
313315

316+
const kSameContextTargets = ["_self", "_parent", "_top"];
317+
314318
// Options may also need computation / resolution before being handed
315319
// off to the template. This function will do any computation on the options
316320
// so they're ready for the template
@@ -394,6 +398,19 @@ export function reshapeListing(
394398
utilities.fieldName = (field: string) => {
395399
return reshaped[kFieldDisplayNames][field] || field;
396400
};
401+
const targetAttrs = (item: ListingItem) => {
402+
const asValue = (value: unknown) =>
403+
typeof value === "string" && value !== "" ? value : undefined;
404+
const target = asValue(item[kTarget]) ?? asValue(listing[kTarget]);
405+
const explicitRel = asValue(item[kRel]) ?? asValue(listing[kRel]);
406+
const rel = explicitRel ??
407+
(target && !kSameContextTargets.includes(target.toLowerCase())
408+
? "noopener"
409+
: undefined);
410+
return (target ? ` target="${encodeAttributeValue(target)}"` : "") +
411+
(rel ? ` rel="${encodeAttributeValue(rel)}"` : "");
412+
};
413+
utilities.targetAttrs = targetAttrs;
397414
utilities.outputLink = (
398415
item: ListingItem,
399416
field: string,
@@ -404,9 +421,9 @@ export function reshapeListing(
404421
const value = val || item[field] || "&nbsp;";
405422
const path = item.path;
406423
if (path && value !== undefined && fieldLinks.includes(field)) {
407-
return `<a href="${path}" class="${field}${
408-
clz ? ` ${clz}` : ""
409-
}">${value}</a>`;
424+
return `<a href="${encodeAttributeValue(path)}"${
425+
targetAttrs(item)
426+
} class="${field}${clz ? ` ${clz}` : ""}">${value}</a>`;
410427
} else {
411428
if (clz) {
412429
return `<span class="${clz}">${value}</span>`;

‎src/resources/editor/tools/vs-code.mjs‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11002,6 +11002,16 @@ var init_yaml_intelligence_resources = __esm({
1100211002
exclude: {
1100311003
maybeArrayOf: "object",
1100411004
description: "Items with matching field values will be excluded from the listing."
11005+
},
11006+
rel: {
11007+
string: {
11008+
description: "Value for rel attribute. Multiple space-separated values are permitted.\nSee <https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel>\nfor details.\n"
11009+
}
11010+
},
11011+
target: {
11012+
string: {
11013+
description: "Value for target attribute.\nSee <https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-target>\nfor details.\n"
11014+
}
1100511015
}
1100611016
}
1100711017
}
@@ -11015,7 +11025,17 @@ var init_yaml_intelligence_resources = __esm({
1101511025
},
1101611026
date: "string",
1101711027
title: "string",
11018-
subtitle: "string"
11028+
subtitle: "string",
11029+
rel: {
11030+
string: {
11031+
description: "Value for rel attribute. Multiple space-separated values are permitted.\nSee <https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel>\nfor details.\n"
11032+
}
11033+
},
11034+
target: {
11035+
string: {
11036+
description: "Value for target attribute.\nSee <https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-target>\nfor details.\n"
11037+
}
11038+
}
1101911039
}
1102011040
}
1102111041
},
@@ -22790,6 +22810,10 @@ var init_yaml_intelligence_resources = __esm({
2279022810
},
2279122811
"Items with matching field values will be included in the listing.",
2279222812
"Items with matching field values will be excluded from the\nlisting.",
22813+
'Value for rel attribute. Multiple space-separated values are\npermitted. See <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel" class="uri">https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel</a>\nfor details.',
22814+
'Value for target attribute. See <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-target" class="uri">https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-target</a>\nfor details.',
22815+
'Value for rel attribute. Multiple space-separated values are\npermitted. See <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel" class="uri">https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel</a>\nfor details.',
22816+
'Value for target attribute. See <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-target" class="uri">https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-target</a>\nfor details.',
2279322817
"The year",
2279422818
"The month",
2279522819
"The day",
@@ -25824,12 +25848,12 @@ var init_yaml_intelligence_resources = __esm({
2582425848
mermaid: "%%"
2582525849
},
2582625850
"handlers/mermaid/schema.yml": {
25827-
_internalId: 221299,
25851+
_internalId: 221307,
2582825852
type: "object",
2582925853
description: "be an object",
2583025854
properties: {
2583125855
"mermaid-format": {
25832-
_internalId: 221291,
25856+
_internalId: 221299,
2583325857
type: "enum",
2583425858
enum: [
2583525859
"png",
@@ -25845,7 +25869,7 @@ var init_yaml_intelligence_resources = __esm({
2584525869
exhaustiveCompletions: true
2584625870
},
2584725871
theme: {
25848-
_internalId: 221298,
25872+
_internalId: 221306,
2584925873
type: "anyOf",
2585025874
anyOf: [
2585125875
{

‎src/resources/editor/tools/yaml/all-schema-definitions.json‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎src/resources/editor/tools/yaml/web-worker.js‎

Lines changed: 28 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/resources/editor/tools/yaml/yaml-intelligence-resources.json‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3975,6 +3975,16 @@
39753975
"exclude": {
39763976
"maybeArrayOf": "object",
39773977
"description": "Items with matching field values will be excluded from the listing."
3978+
},
3979+
"rel": {
3980+
"string": {
3981+
"description": "Value for rel attribute. Multiple space-separated values are permitted.\nSee <https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel>\nfor details.\n"
3982+
}
3983+
},
3984+
"target": {
3985+
"string": {
3986+
"description": "Value for target attribute.\nSee <https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-target>\nfor details.\n"
3987+
}
39783988
}
39793989
}
39803990
}
@@ -3988,7 +3998,17 @@
39883998
},
39893999
"date": "string",
39904000
"title": "string",
3991-
"subtitle": "string"
4001+
"subtitle": "string",
4002+
"rel": {
4003+
"string": {
4004+
"description": "Value for rel attribute. Multiple space-separated values are permitted.\nSee <https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel>\nfor details.\n"
4005+
}
4006+
},
4007+
"target": {
4008+
"string": {
4009+
"description": "Value for target attribute.\nSee <https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-target>\nfor details.\n"
4010+
}
4011+
}
39924012
}
39934013
}
39944014
},
@@ -15763,6 +15783,10 @@
1576315783
},
1576415784
"Items with matching field values will be included in the listing.",
1576515785
"Items with matching field values will be excluded from the\nlisting.",
15786+
"Value for rel attribute. Multiple space-separated values are\npermitted. See <a href=\"https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel\" class=\"uri\">https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel</a>\nfor details.",
15787+
"Value for target attribute. See <a href=\"https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-target\" class=\"uri\">https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-target</a>\nfor details.",
15788+
"Value for rel attribute. Multiple space-separated values are\npermitted. See <a href=\"https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel\" class=\"uri\">https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel</a>\nfor details.",
15789+
"Value for target attribute. See <a href=\"https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-target\" class=\"uri\">https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-target</a>\nfor details.",
1576615790
"The year",
1576715791
"The month",
1576815792
"The day",
@@ -18797,12 +18821,12 @@
1879718821
"mermaid": "%%"
1879818822
},
1879918823
"handlers/mermaid/schema.yml": {
18800-
"_internalId": 221299,
18824+
"_internalId": 221307,
1880118825
"type": "object",
1880218826
"description": "be an object",
1880318827
"properties": {
1880418828
"mermaid-format": {
18805-
"_internalId": 221291,
18829+
"_internalId": 221299,
1880618830
"type": "enum",
1880718831
"enum": [
1880818832
"png",
@@ -18818,7 +18842,7 @@
1881818842
"exhaustiveCompletions": true
1881918843
},
1882018844
"theme": {
18821-
"_internalId": 221298,
18845+
"_internalId": 221306,
1882218846
"type": "anyOf",
1882318847
"anyOf": [
1882418848
{

0 commit comments

Comments
 (0)