Skip to content
Open
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
21 changes: 14 additions & 7 deletions js/src/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class Link {
}


format(lessParentheses = false, isCompoundValue = false) {
format(lessParentheses = false, isCompoundValue = false, arrowMode = false) {
// Empty link
if (this.id === null && (!this.values || this.values.length === 0)) {
return lessParentheses ? '' : '()';
Expand All @@ -94,10 +94,17 @@ export class Link {
}

// Format values recursively
const valuesStr = this.values.map(v => this.formatValue(v)).join(' ');
const valuesStr = this.values.map(v => this.formatValue(v, arrowMode)).join(' ');

// Link with values only (null id)
if (this.id === null) {
// Arrow mode formatting for directional links
if (arrowMode && this.values && this.values.length === 2) {
const left = this.formatValue(this.values[0], arrowMode);
const right = this.formatValue(this.values[1], arrowMode);
return `${left} → ${right}`;
}

// For lessParentheses mode with simple values, don't wrap the whole thing
if (lessParentheses) {
// Check if all values are simple (no nested values)
Expand All @@ -121,7 +128,7 @@ export class Link {
return lessParentheses && !this.needsParentheses(this.id) ? withColon : `(${withColon})`;
}

formatValue(value) {
formatValue(value, arrowMode = false) {
if (!value.format) {
return Link.escapeReference(value.id || '');
}
Expand All @@ -132,7 +139,7 @@ export class Link {

// For compound links from paths, format values with parentheses
if (isCompoundFromPaths) {
return value.format(false, true);
return value.format(false, true, arrowMode);
}

// Simple link with just an ID - don't wrap in parentheses when used as a value
Expand All @@ -141,15 +148,15 @@ export class Link {
}

// Complex value with its own structure - format it normally with parentheses
return value.format(false, false);
return value.format(false, false, arrowMode);
}

needsParentheses(str) {
return str && (str.includes(' ') || str.includes(':') || str.includes('(') || str.includes(')'));
}
}

export function formatLinks(links, lessParentheses = false) {
export function formatLinks(links, lessParentheses = false, arrowMode = false) {
if (!links || links.length === 0) return '';
return links.map(link => link.format(lessParentheses)).join('\n');
return links.map(link => link.format(lessParentheses, false, arrowMode)).join('\n');
}
27 changes: 25 additions & 2 deletions js/src/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,22 @@ referenceOrLink = l:multiLineAnyLink { return l; } / i:reference { return { id:

anyLink = ml:multiLineAnyLink eol { return ml; } / sl:singleLineAnyLink { return sl; }

multiLineAnyLink = multiLineValueLink / multiLineLink
multiLineAnyLink = multiLineValueLink / multiLineLink / multiLineArrowLink

// Multiline arrow syntax support
multiLineArrowLink = "(" _ left:referenceOrLink __ arrow:("←" / "→") __ right:referenceOrLink _ ")" {
if (arrow === "←") {
// left ← right means right points to left
return { values: [right, left] };
} else {
// left → right means left points to right
return { values: [left, right] };
}
}

singleLineAnyLink = fl:singleLineLink eol { return fl; }
/ vl:singleLineValueLink eol { return vl; }
/ al:arrowLink eol { return al; }

multiLineValueAndWhitespace = value:referenceOrLink _ { return value; }

Expand All @@ -51,6 +63,17 @@ singleLineValueAndWhitespace = __ value:referenceOrLink { return value; }

singleLineValues = list:singleLineValueAndWhitespace+ { return list; }

// Arrow syntax support for directional links
arrowLink = left:referenceOrLink __ arrow:("←" / "→") __ right:referenceOrLink {
if (arrow === "←") {
// left ← right means right points to left
return { values: [right, left] };
} else {
// left → right means left points to right
return { values: [left, right] };
}
}

singleLineLink = __ id:reference __ ":" v:singleLineValues { return { id: id, values: v }; }

multiLineLink = "(" _ id:reference _ ":" v:multiLineValues _ ")" { return { id: id, values: v }; }
Expand Down Expand Up @@ -81,4 +104,4 @@ _ = whiteSpaceSymbol*

whiteSpaceSymbol = [ \t\n\r]

referenceSymbol = [^ \t\n\r(:)]
referenceSymbol = [^ \t\n\r(:)←→]
Loading
Loading