Skip to content
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
20 changes: 10 additions & 10 deletions packages/parser/src/parser/rules/inline/link-triple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
* - Interwiki links: `[[[wikipedia:Article]]]` (for known prefixes)
*
* Special syntax:
* - `[[[*page]]]` -- `*` prefix is treated as a label prefix (ignored in target)
* - `[[[*target]]]` -- `*` prefix is stripped from target; for external URLs,
* adds `target="_blank"` (new tab)
* - `[[[*|label]]]` -- links to root `/` with the given label
* - `[[[page|]]]` -- empty label after pipe defaults to the page name
*
Expand Down Expand Up @@ -86,7 +87,7 @@ function hasClosingLinkMarker(ctx: ParseContext, startPos: number): boolean {
* - Empty target with pipe (`[[[|text]]]`) is invalid
* - Multiple consecutive `#` in the target (`[[[page##anchor]]]`) is invalid
* - `[[[*|label]]]` links to root `/`
* - `[[[*page]]]` strips the `*` prefix from the target
* - `[[[*target]]]` strips `*`; adds `target="_blank"` for external URLs
* - Category pages show only the text after the colon when no label is given
*/
export const linkTripleRule: InlineRule = {
Expand Down Expand Up @@ -174,13 +175,11 @@ export const linkTripleRule: InlineRule = {
};
}

// Special case: [[[*|label]]] means link to root "/" with label
// `*` prefix: stripped from target; sets target="_blank" for external URLs
let finalTarget = trimmedTarget;
if (trimmedTarget === "*" && foundPipe) {
finalTarget = "";
}
// Special case: [[[*page]]] - * is a label prefix, page is the target
if (trimmedTarget.startsWith("*") && !foundPipe) {
let hasStar = false;
if (trimmedTarget.startsWith("*")) {
hasStar = true;
finalTarget = trimmedTarget.slice(1);
}

Expand All @@ -194,8 +193,9 @@ export const linkTripleRule: InlineRule = {
displayText = trimmedLabel || finalTarget;
} else {
// For category pages (system:Recent Changes), use only the part after colon
// Use trimmedTarget (preserves * prefix) for display when no pipe
const colonIdx = trimmedTarget.indexOf(":");
if (colonIdx !== -1 && !trimmedTarget.startsWith("http")) {
if (colonIdx !== -1 && !trimmedTarget.startsWith("http") && !trimmedTarget.startsWith("*")) {
displayText = trimmedTarget.slice(colonIdx + 1).trim();
} else {
displayText = trimmedTarget;
Expand All @@ -214,7 +214,7 @@ export const linkTripleRule: InlineRule = {
link,
extra: null,
label,
target: null,
target: hasStar && linkType === "direct" ? "new-tab" : null,
},
},
],
Expand Down
78 changes: 78 additions & 0 deletions tests/fixtures/link/triple/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,84 @@
]
}
},
{
"element": "container",
"data": {
"type": "paragraph",
"attributes": {},
"elements": [
{
"element": "container",
"data": {
"type": "bold",
"attributes": {},
"elements": [
{
"element": "text",
"data": "Star"
},
{
"element": "text",
"data": " "
},
{
"element": "text",
"data": "prefix"
}
]
}
}
]
}
},
{
"element": "list",
"data": {
"type": "bullet",
"attributes": {},
"items": [
{
"item-type": "elements",
"attributes": {},
"elements": [
{
"element": "link",
"data": {
"type": "direct",
"link": "http://example.com",
"extra": null,
"label": {
"text": "example"
},
"target": "new-tab"
}
}
]
},
{
"item-type": "elements",
"attributes": {},
"elements": [
{
"element": "link",
"data": {
"type": "page",
"link": {
"site": null,
"page": "test"
},
"extra": null,
"label": {
"text": "*test"
},
"target": null
}
}
]
}
]
}
},
{
"element": "footnote-block",
"data": {
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/link/triple/input.ftml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@

**URL**
* [[[https://example.com/|Example]]]

**Star prefix**
* [[[*http://example.com|example]]]
* [[[*test|*test]]]
5 changes: 5 additions & 0 deletions tests/fixtures/link/triple/output.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@
<ul>
<li><a href="https://example.com/">Example</a></li>
</ul>
<p><strong>Star prefix</strong></p>
<ul>
<li><a href="http://example.com" target="_blank" rel="noopener noreferrer">example</a></li>
<li><a class="newpage" href="/test">*test</a></li>
</ul>
5 changes: 5 additions & 0 deletions tests/fixtures/link/triple/wikidot.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@
<ul>
<li><a href="https://example.com/">Example</a></li>
</ul>
<p><strong>Star prefix</strong></p>
<ul>
<li><a target="_blank" href="http://example.com">example</a></li>
<li><a href="/test">*test</a></li>
</ul>