Skip to content

Commit

Permalink
Normalize reference + footnote labels.
Browse files Browse the repository at this point in the history
Trim and collapse adjacent interior whitespace, including
newlines, to a single space.

This will allow implicit heading references to work even when
headings span multiple lines (#63).
  • Loading branch information
jgm committed Sep 27, 2023
1 parent 434d84a commit ee172ef
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ enum Context {
Literal = 2 // gather str, soft_break, hard_break in accumulatedText
}

const normalizeLabel = function(label : string): string {
return label.trim().replace(/[ \t\r\n]+/, " ")
}

const parse = function(input: string, options: ParseOptions = {}): Doc {

const linestarts: number[] = [-1];
Expand Down Expand Up @@ -291,7 +295,8 @@ const parse = function(input: string, options: ParseOptions = {}): Doc {

footnote_reference: (suffixes, startpos, endpos, pos) => {
const fnref = input.substring(startpos + 2, endpos);
addChildToTip({ tag: "footnote_reference", text: fnref, pos: pos});
addChildToTip({ tag: "footnote_reference",
text: normalizeLabel(fnref), pos: pos});
},

["+reference_definition"]: (suffixes, startpos, endpos, pos) => {
Expand All @@ -300,14 +305,15 @@ const parse = function(input: string, options: ParseOptions = {}): Doc {

["-reference_definition"]: (suffixes, startpos, endpos, pos) => {
const node = popContainer(pos);
const lab = normalizeLabel(node.data.key);
const r: Reference = {
tag: "reference",
label: node.data.key,
label: lab,
destination: node.data.value || "",
attributes: node.attributes
};
if (node.data.key) {
references[node.data.key] = r;
references[lab] = r;
}
},

Expand Down Expand Up @@ -611,7 +617,7 @@ const parse = function(input: string, options: ParseOptions = {}): Doc {

["-reference"]: (suffixes, startpos, endpos, pos) => {
const node = popContainer(pos);
const txt = input.substring(node.data.startReference + 1, startpos);
const txt = normalizeLabel(input.substring(node.data.startReference + 1, startpos));
if (containers.length > 0) {
const tip = containers[containers.length - 1];
// the container added by +linktext or +imagetext
Expand Down Expand Up @@ -751,10 +757,11 @@ const parse = function(input: string, options: ParseOptions = {}): Doc {
identifiers[node.attributes.id] = true;
}
// add implicit heading reference
if (!references[headingStr]) {
references[headingStr] = {
const lab = normalizeLabel(headingStr);
if (!references[lab]) {
references[lab] = {
tag: "reference",
label: headingStr,
label: lab,
destination: "#" + node.attributes.id
};
}
Expand Down Expand Up @@ -1041,10 +1048,11 @@ const parse = function(input: string, options: ParseOptions = {}): Doc {
["-footnote"]: (suffixes, startpos, endpos, pos) => {
const node = popContainer(pos);
if (node.data.label) {
footnotes[node.data.label] =
const lab = normalizeLabel(node.data.label);
footnotes[lab] =
{
tag: "footnote",
label: node.data.label || "",
label: lab || "",
children: node.children,
attributes: node.attributes,
pos: node.pos
Expand Down

0 comments on commit ee172ef

Please sign in to comment.