Skip to content

Commit

Permalink
content reference parser - parse references on new lines correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
KDKHD committed Feb 6, 2025
1 parent 55ef292 commit 015ac28
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ describe('ContentReferenceParser', () => {
A furry friend, from head to toes.{reference(ccaSI)}
Loyal companion, always near,{reference(ccaSI)}
Chasing squirrels, full of cheer.{reference(ccaSI)}
A paw to hold, a gentle nudge,{reference(ccaSI)}
A paw to hold, a gentle nudge,
{reference(ccaSI)}
A furry alarm, a playful judge.{reference(ccaSI)}
From golden retrievers to tiny Chihuahuas,{reference(ccaSI)}
Their love's a gift, that always conquers.{reference(ccaSI)}
Expand All @@ -29,6 +30,24 @@ Their love's a beacon, shining bright.{reference(ccaSI)}`) as Parent;
(child) => (child.type as string) === 'contentReference'
)
).toHaveLength(10);
expect(file.children[0].children).toEqual(
expect.arrayContaining([
expect.objectContaining({ type: 'text', value: '\nA paw to hold, a gentle nudge,\n' }),
])
);
});

it('extracts reference after linebreak', async () => {
const file = unified().use([[markdown, {}], ContentReferenceParser]).parse(`First line
{reference(FTQJp)}
`) as Parent;

expect(file.children[0].children).toEqual(
expect.arrayContaining([
expect.objectContaining({ type: 'text', value: 'First line\n' }),
expect.objectContaining({ type: 'contentReference' }),
])
);
});

it('eats empty content reference', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export interface ContentReferenceNode extends Node {
contentReferenceBlock: ContentReferenceBlock;
}

/**
* Parses `{reference(contentReferenceId)}` or ` {reference(contentReferenceId)}` (notice space prefix) into ContentReferenceNode
*/
/** Matches `{reference` and ` {reference(` */
const REFERENCE_START_PATTERN = '\\u0020?\\{reference';

export const ContentReferenceParser: Plugin = function ContentReferenceParser() {
const Parser = this.Parser;
const tokenizers = Parser.prototype.inlineTokenizers;
Expand All @@ -33,7 +33,7 @@ export const ContentReferenceParser: Plugin = function ContentReferenceParser()
value,
silent
) {
const [match] = value.match(/^\s?{reference/) || [];
const [match] = value.match(new RegExp(`^${REFERENCE_START_PATTERN}`)) || [];

if (!match) return false;

Expand Down Expand Up @@ -115,7 +115,9 @@ export const ContentReferenceParser: Plugin = function ContentReferenceParser()
tokenizeCustomCitation.notInLink = true;

tokenizeCustomCitation.locator = (value, fromIndex) => {
return 1 + (value.substring(fromIndex).match(/\s?{reference/)?.index ?? -2);
return (
1 + (value.substring(fromIndex).match(new RegExp(`${REFERENCE_START_PATTERN}`))?.index ?? -2)
);
};

tokenizers.contentReference = tokenizeCustomCitation;
Expand Down

0 comments on commit 015ac28

Please sign in to comment.