Skip to content

Add parseInlineSegments function to fix links with formatting#3335

Open
juliaroldi wants to merge 2 commits intomasterfrom
u/juliaroldi/markdown-links
Open

Add parseInlineSegments function to fix links with formatting#3335
juliaroldi wants to merge 2 commits intomasterfrom
u/juliaroldi/markdown-links

Conversation

@juliaroldi
Copy link
Copy Markdown
Contributor

Consolidates the markdown inline-segment parsing logic into a single recursive parser, replacing the previous split between splitParagraphSegments, applyTextFormatting, and applyLink. This fixes a class of bugs where formatting state was not preserved across link/image boundaries (e.g. link), and simplifies the overall pipeline.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the markdown-to-content-model inline parsing pipeline by replacing the previous split/link/formatting appliers with a single recursive inline parser, aiming to preserve formatting state across link boundaries (e.g. **[link](url)**) and simplify segment generation.

Changes:

  • Replaced the old inline pipeline (splitParagraphSegments + applyLink + applyTextFormatting) with a consolidated recursive parser (parseInlineSegments).
  • Updated applySegmentFormatting to build segments via parseInlineSegments, then apply heading adjustment to the first text segment.
  • Reworked test coverage by removing tests for deleted utilities/appliers and adding new parseInlineSegments tests plus new link-formatting cases in applySegmentFormatting tests.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
packages/roosterjs-content-model-markdown/test/markdownToModel/utils/splitParagraphSegmentsTest.ts Removed tests for deleted splitParagraphSegments utility.
packages/roosterjs-content-model-markdown/test/markdownToModel/utils/parseInlineSegmentsTest.ts Added unit tests for the new consolidated inline parser (formatting, links, images).
packages/roosterjs-content-model-markdown/test/markdownToModel/appliers/applyTextFormattingTest.ts Removed tests for deleted applyTextFormatting applier.
packages/roosterjs-content-model-markdown/test/markdownToModel/appliers/applySegmentFormattingTest.ts Added scenarios validating formatting behavior inside/around markdown links.
packages/roosterjs-content-model-markdown/test/markdownToModel/appliers/applyLinkTest.ts Removed tests for deleted applyLink applier.
packages/roosterjs-content-model-markdown/lib/markdownToModel/utils/splitParagraphSegments.ts Deleted old link/image splitting utility in favor of the new unified parser.
packages/roosterjs-content-model-markdown/lib/markdownToModel/utils/parseInlineSegments.ts Added new recursive parser that produces Content Model segments directly (text/link/image + formatting state).
packages/roosterjs-content-model-markdown/lib/markdownToModel/appliers/applyTextFormatting.ts Deleted old formatting-only applier, now superseded by parseInlineSegments.
packages/roosterjs-content-model-markdown/lib/markdownToModel/appliers/applySegmentFormatting.ts Updated to use parseInlineSegments and apply heading adjustment to the first text segment.
packages/roosterjs-content-model-markdown/lib/markdownToModel/appliers/applyLink.ts Deleted old link-only applier, now superseded by parseInlineSegments.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +71 to +75
const innerLink: ContentModelLink = {
dataset: {},
format: { href: linkMatch[2], underline: true },
};
parseInlineSegments(linkMatch[1], segments, state, innerLink);
Comment on lines +82 to +86
if (marker && shouldToggleFormatting(text, i, marker, state)) {
flushBuffer();
toggleFormatting(state, marker.type);
i += marker.length;
continue;
Comment on lines +55 to +59
while (i < text.length) {
const remaining = text.substring(i);

// Image: ![alt](url)
const imgMatch = imagePattern.exec(remaining);
Comment on lines +138 to +142
it('should parse formatting inside a link', () => {
runTest('[**bold**](https://example.com)', [
{
segmentType: 'Text',
text: 'bold',
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants