fix(markdown-parser, formatter): correctly parse links
MODIFY
internal/ast/index.ts
internal/ast/index.ts
@@ -12,7 +12,7 @@ export * from "./unions";
export {bindingKeys, nodeNames, visitorKeys} from "./utils";
export type AnyNodes = AnyNode | (AnyNode[]);
-/* GENERATED:START(hash:6451ba35633a1ebbf85e8a824f4a8a29cbd05309,id:main) Everything below is automatically generated. DO NOT MODIFY. Run `./rome run scripts/generated-files/ast` to update. */
+/* GENERATED:START(hash:120ed13bebb310f3482e342f82be74b1a0a8eeff,id:main) Everything below is automatically generated. DO NOT MODIFY. Run `./rome run scripts/generated-files/ast` to update. */
export * from "./common/comments/CommentBlock";
export * from "./common/comments/CommentLine";
export * from "./css/core/CSSAtRule";
@@ -233,11 +233,11 @@ export * from "./markdown/blocks/MarkdownDividerBlock";
export * from "./markdown/inline/MarkdownEmphasisInline";
export * from "./markdown/blocks/MarkdownHeadingBlock";
export * from "./markdown/inline/MarkdownImageInline";
+export * from "./markdown/inline/MarkdownLink";
export * from "./markdown/blocks/MarkdownListBlock";
export * from "./markdown/core/MarkdownListItem";
export * from "./markdown/core/MarkdownParagraph";
export * from "./markdown/blocks/MarkdownQuoteBlock";
-export * from "./markdown/inline/MarkdownReferenceInline";
export * from "./markdown/core/MarkdownRoot";
export * from "./markdown/core/MarkdownText";
export * from "./common/core/MockParent";
@@ -534,11 +534,11 @@ export type AnyNode =
| n.MarkdownEmphasisInline
| n.MarkdownHeadingBlock
| n.MarkdownImageInline
+ | n.MarkdownLink
| n.MarkdownListBlock
| n.MarkdownListItem
| n.MarkdownParagraph
| n.MarkdownQuoteBlock
- | n.MarkdownReferenceInline
| n.MarkdownRoot
| n.MarkdownText
| n.MockParent

ADD
internal/ast/markdown/inline/MarkdownLink.ts
+import {MarkdownReference, NodeBaseWithComments} from "@internal/ast";
+import {createBuilder} from "../../utils";
+
+// [link](www.example.com)
+export interface MarkdownLink extends NodeBaseWithComments {
+ type: "MarkdownLink";
+ text?: MarkdownReference;
+ title?: string;
+ link: string;
+}
+
+export const markdownLink = createBuilder<MarkdownLink>(
+ "MarkdownLink",
+ {
+ bindingKeys: {},
+ visitorKeys: {
+ text: true,
+ },
+ },
+);

DELETE
internal/ast/markdown/inline/MarkdownReferenceInline.ts
-import {MarkdownReference, NodeBaseWithComments} from "@internal/ast";
-import {createBuilder} from "../../utils";
-
-// [link](www.example.com)
-export interface MarkdownReferenceInline extends NodeBaseWithComments {
- type: "MarkdownReferenceInline";
- value: string;
- reference?: MarkdownReference;
- title?: string;
-}
-
-export const markdownReferenceInline = createBuilder<MarkdownReferenceInline>(
- "MarkdownReferenceInline",
- {
- bindingKeys: {},
- visitorKeys: {
- reference: true,
- },
- },
-);

MODIFY
internal/ast/markdown/unions.ts
internal/ast/markdown/unions.ts
@@ -19,7 +19,7 @@ export type AnyMarkdownInlineNode =
| n.MarkdownBoldInline
| n.MarkdownCodeInline
| n.MarkdownAutomaticLinkInline
- | n.MarkdownReferenceInline
+ | n.MarkdownLink
| n.MarkdownImageInline
| n.MarkdownEmphasisInline;
@@ -35,6 +35,6 @@ export type AnyMarkdownNode =
| n.MarkdownBoldInline
| n.MarkdownCodeInline
| n.MarkdownAutomaticLinkInline
- | n.MarkdownReferenceInline
+ | n.MarkdownLink
| n.MarkdownImageInline
| n.MarkdownEmphasisInline;

MODIFY
internal/formatter/builders/index.ts
internal/formatter/builders/index.ts
@@ -14,7 +14,7 @@ const builders: ExtendedMap<string, BuilderMethod<any>> = new ExtendedMap(
);
export default builders;
-/* GENERATED:START(hash:7a8ec38bd9c62bb0b3bc19e881f1f3f2f1096159,id:main) Everything below is automatically generated. DO NOT MODIFY. Run `./rome run scripts/generated-files/ast` to update. */
+/* GENERATED:START(hash:02bf1b74e6c258a579b043c22390fc7de5a1d7fa,id:main) Everything below is automatically generated. DO NOT MODIFY. Run `./rome run scripts/generated-files/ast` to update. */
import CommentBlock from "./common/comments/CommentBlock";
builders.set("CommentBlock", CommentBlock);
import CommentLine from "./common/comments/CommentLine";
@@ -464,6 +464,8 @@ import MarkdownHeadingBlock from "./markdown/blocks/MarkdownHeadingBlock";
builders.set("MarkdownHeadingBlock", MarkdownHeadingBlock);
import MarkdownImageInline from "./markdown/inline/MarkdownImageInline";
builders.set("MarkdownImageInline", MarkdownImageInline);
+import MarkdownLink from "./markdown/inline/MarkdownLink";
+builders.set("MarkdownLink", MarkdownLink);
import MarkdownListBlock from "./markdown/blocks/MarkdownListBlock";
builders.set("MarkdownListBlock", MarkdownListBlock);
import MarkdownListItem from "./markdown/core/MarkdownListItem";
@@ -472,8 +474,6 @@ import MarkdownParagraph from "./markdown/core/MarkdownParagraph";
builders.set("MarkdownParagraph", MarkdownParagraph);
import MarkdownQuoteBlock from "./markdown/blocks/MarkdownQuoteBlock";
builders.set("MarkdownQuoteBlock", MarkdownQuoteBlock);
-import MarkdownReferenceInline from "./markdown/inline/MarkdownReferenceInline";
-builders.set("MarkdownReferenceInline", MarkdownReferenceInline);
import MarkdownRoot from "./markdown/core/MarkdownRoot";
builders.set("MarkdownRoot", MarkdownRoot);
import MarkdownText from "./markdown/core/MarkdownText";

ADD
internal/formatter/builders/markdown/inline/MarkdownLink.ts
+import {MarkdownLink} from "@internal/ast";
+import {Builder, Token, concat, space} from "@internal/formatter";
+
+export default function MarkdownLink(
+ builder: Builder,
+ node: MarkdownLink,
+): Token {
+ const tokens: Token[] = ["["];
+ if (node.text && node.text.length > 0) {
+ tokens.push(
+ concat(
+ node.text.map((ref) => {
+ return builder.tokenize(ref, node);
+ }),
+ ),
+ );
+ }
+ tokens.push("]");
+
+ tokens.push("(");
+ tokens.push(node.link);
+ if (node.title) {
+ tokens.push(space);
+ tokens.push(node.title);
+ }
+ tokens.push(")");
+
+ return concat(tokens);
+}

DELETE
internal/formatter/builders/markdown/inline/MarkdownReferenceInline.ts
-import {MarkdownReferenceInline} from "@internal/ast";
-import {Builder, Token, concat} from "@internal/formatter";
-
-export default function MarkdownReferenceInline(
- builder: Builder,
- node: MarkdownReferenceInline,
-): Token {
- const tokens: Token[] = ["["];
- if (node.title) {
- tokens.push(node.title);
- }
- tokens.push("]");
-
- if (node.reference && node.reference.length > 0) {
- tokens.push("(");
-
- tokens.push(
- concat(
- node.reference.map((ref) => {
- return builder.tokenize(ref, node);
- }),
- ),
- );
-
- tokens.push(")");
- }
-
- return concat(tokens);
-}

MODIFY
internal/markdown-parser/index.ts
internal/markdown-parser/index.ts
@@ -26,7 +26,7 @@ import {
} from "@internal/markdown-parser/parser/listItem";
import {tokenizeInline} from "@internal/markdown-parser/parser/inline";
import {parseParagraph} from "@internal/markdown-parser/parser/paragraph";
-import {parseReference} from "@internal/markdown-parser/parser/reference";
+import {parseLink} from "@internal/markdown-parser/parser/link";
export type MarkdownParser = ParserCore<MarkdownParserTypes>;
@@ -307,7 +307,7 @@ function parseBlock(
return undefined;
}
case "OpenSquareBracket": {
- return parseReference(parser);
+ return parseLink(parser);
}
case "HeadingLevel":
return parseHeading(parser);

RENAME
internal/markdown-parser/parser/reference.ts
internal/markdown-parser/parser/link.ts
import {MarkdownParser} from "@internal/markdown-parser";
import {
AnyMarkdownInlineNode,
+ MarkdownLink,
MarkdownReference,
- MarkdownReferenceInline,
} from "@internal/ast";
import {parseInline} from "@internal/markdown-parser/parser/inline";
import {descriptions} from "@internal/diagnostics";
import {parseText} from "@internal/markdown-parser/parser/text";
-export function parseReference(
+export function parseLink(
parser: MarkdownParser,
-): MarkdownReferenceInline | AnyMarkdownInlineNode[] {
+): MarkdownLink | AnyMarkdownInlineNode[] {
parser.expectToken("OpenSquareBracket");
const pos = parser.getPosition();
@@ -24,15 +24,53 @@ export function parseReference(
description: descriptions.MARKDOWN_PARSER.ONLY_TEXT_INSIDE_DEFINITIONS,
});
}
+
+ const closeSquarePos = parser.getPosition();
parser.eatToken("CloseSquareBracket");
+ if (parser.getToken().type === "OpenBracket") {
+ parser.eatToken("OpenBracket");
+ const link = parser.eatToken("Text");
+ const closeBracket = parser.eatToken("CloseBracket");
+ if (link && closeBracket) {
return parser.finishNode(
pos,
{
- type: "MarkdownReferenceInline",
- value: "",
- reference: reference as MarkdownReference,
+ type: "MarkdownLink",
+ text: reference as MarkdownReference,
+ link: link.value,
},
);
+ } else {
+ reference.push(
+ parser.finishNode(
+ pos,
+ {
+ type: "MarkdownText",
+ value: `(${link?.value}`,
+ },
+ ),
+ );
+ return [...(reference as AnyMarkdownInlineNode[])];
+ }
+ } else {
+ return [
+ parser.finishNode(
+ pos,
+ {
+ type: "MarkdownText",
+ value: "[",
+ },
+ ),
+ ...(reference as AnyMarkdownInlineNode[]),
+ parser.finishNode(
+ closeSquarePos,
+ {
+ type: "MarkdownText",
+ value: "]",
+ },
+ ),
+ ];
+ }
}
const token = parser.getToken();
if (token.type === "Text") {

MODIFY
internal/markdown-parser/parser/paragraph.ts
internal/markdown-parser/parser/paragraph.ts
@@ -3,7 +3,7 @@ import {MarkdownParser, isBlockToken} from "@internal/markdown-parser";
import {parseInline} from "@internal/markdown-parser/parser/inline";
import {descriptions} from "@internal/diagnostics";
import {parseText} from "@internal/markdown-parser/parser/text";
-import {parseReference} from "@internal/markdown-parser/parser/reference";
+import {parseLink} from "@internal/markdown-parser/parser/link";
import {Position} from "@internal/parser-core";
export function parseParagraph(
@@ -49,7 +49,7 @@ export function parseParagraph(
// TODO: to add support for more inline tokens: link, code inline block
(unknownToken) => {
if (unknownToken.type === "OpenSquareBracket") {
- return parseReference(parser);
+ return parseLink(parser);
}
return parseText(parser);
@@ -66,7 +66,7 @@ export function parseParagraph(
break;
}
case "OpenSquareBracket": {
- const reference = parseReference(parser);
+ const reference = parseLink(parser);
if (Array.isArray(reference)) {
children.push(...reference);
} else {

ADD
internal/markdown-parser/test-fixtures/link/input.md
+[lorem](/uri)
+
+[*lorem*](/uri)
+
+[**ipsum**](/uri)
+
+[__lorem__](/uri)
+
+[_lorem_](/uri)
+
+[lorem* ipsum**](/uri)
+
+[*lorem **ipsum](/uri)
+

ADD
internal/markdown-parser/test-fixtures/link/input.test.md
+# `index.test.ts`
+
+**DO NOT MODIFY**. This file has been autogenerated. Run `rome test internal/markdown-parser/index.test.ts --update-snapshots` to update.
+
+## `link`
+
+### `ast`
+
+```javascript
+MarkdownRoot {
+ body: [
+ MarkdownLink {
+ link: "/uri"
+ text: [
+ MarkdownText {
+ value: "lorem"
+ loc: SourceLocation link/input.md 1:1-1:6
+ }
+ ]
+ loc: SourceLocation link/input.md 1:1-1:13
+ }
+ MarkdownLink {
+ link: "/uri"
+ text: [
+ MarkdownEmphasisInline {
+ value: [
+ MarkdownText {
+ value: "lorem"
+ loc: SourceLocation link/input.md 3:2-3:7
+ }
+ ]
+ loc: SourceLocation link/input.md 3:1-3:8
+ }
+ ]
+ loc: SourceLocation link/input.md 3:1-3:15
+ }
+ MarkdownLink {
+ link: "/uri"
+ text: [
+ MarkdownEmphasisInline {
+ value: [
+ MarkdownText {
+ value: "ipsum"
+ loc: SourceLocation link/input.md 5:3-5:8
+ }
+ ]
+ loc: SourceLocation link/input.md 5:1-5:10
+ }
+ ]
+ loc: SourceLocation link/input.md 5:1-5:17
+ }
+ MarkdownLink {
+ link: "/uri"
+ text: [
+ MarkdownEmphasisInline {
+ value: [
+ MarkdownText {
+ value: "lorem"
+ loc: SourceLocation link/input.md 7:3-7:8
+ }
+ ]
+ loc: SourceLocation link/input.md 7:1-7:10
+ }
+ ]
+ loc: SourceLocation link/input.md 7:1-7:17
+ }
+ MarkdownLink {
+ link: "/uri"
+ text: [
+ MarkdownEmphasisInline {
+ value: [
+ MarkdownText {
+ value: "lorem"
+ loc: SourceLocation link/input.md 9:2-9:7
+ }
+ ]
+ loc: SourceLocation link/input.md 9:1-9:8
+ }
+ ]
+ loc: SourceLocation link/input.md 9:1-9:15
+ }
+ MarkdownLink {
+ link: "/uri"
+ text: [
+ MarkdownText {
+ value: "lorem"
+ loc: SourceLocation link/input.md 11:1-11:6
+ }
+ MarkdownText {
+ value: "*"
+ loc: SourceLocation link/input.md 11:6-11:7
+ }
+ MarkdownText {
+ value: " ipsum"
+ loc: SourceLocation link/input.md 11:7-11:13
+ }
+ MarkdownText {
+ value: "**"
+ loc: SourceLocation link/input.md 11:13-11:15
+ }
+ ]
+ loc: SourceLocation link/input.md 11:1-11:22
+ }
+ MarkdownLink {
+ link: "/uri"
+ text: [
+ MarkdownText {
+ value: "*"
+ loc: SourceLocation link/input.md 13:1-13:2
+ }
+ MarkdownText {
+ value: "lorem "
+ loc: SourceLocation link/input.md 13:2-13:8
+ }
+ MarkdownText {
+ value: "**"
+ loc: SourceLocation link/input.md 13:8-13:10
+ }
+ MarkdownText {
+ value: "ipsum"
+ loc: SourceLocation link/input.md 13:10-13:15
+ }
+ ]
+ loc: SourceLocation link/input.md 13:1-13:22
+ }
+ ]
+ comments: []
+ corrupt: false
+ diagnostics: []
+ path: UIDPath<link/input.md>
+ loc: SourceLocation link/input.md 1:0-14:0
+}
+```
+
+### `diagnostics`
+
+```
+
+```

MODIFY
internal/markdown-parser/test-fixtures/lists/input.test.md
internal/markdown-parser/test-fixtures/lists/input.test.md
@@ -184,9 +184,12 @@ MarkdownRoot {
children: [
MarkdownParagraph {
children: [
- MarkdownReferenceInline {
- value: ""
- reference: []
+ MarkdownText {
+ value: "["
+ loc: SourceLocation lists/input.md 11:3-11:4
+ }
+ MarkdownText {
+ value: "]"
loc: SourceLocation lists/input.md 11:3-11:4
}
MarkdownText {

DELETE
internal/markdown-parser/test-fixtures/reference/input.md
-[lorem]
-
-[*lorem*]
-
-[**ipsum**]
-
-[__lorem__]
-
-[_lorem_]
-
-[lorem* ipsum**]
-
-[*lorem **ipsum]
-

DELETE
internal/markdown-parser/test-fixtures/reference/input.test.md
-# `index.test.ts`
-
-**DO NOT MODIFY**. This file has been autogenerated. Run `rome test internal/markdown-parser/index.test.ts --update-snapshots` to update.
-
-## `reference`
-
-### `ast`
-
-```javascript
-MarkdownRoot {
- body: [
- MarkdownReferenceInline {
- value: ""
- reference: [
- MarkdownText {
- value: "lorem"
- loc: SourceLocation reference/input.md 1:1-1:6
- }
- ]
- loc: SourceLocation reference/input.md 1:1-1:7
- }
- MarkdownReferenceInline {
- value: ""
- reference: [
- MarkdownEmphasisInline {
- value: [
- MarkdownText {
- value: "lorem"
- loc: SourceLocation reference/input.md 3:2-3:7
- }
- ]
- loc: SourceLocation reference/input.md 3:1-3:8
- }
- ]
- loc: SourceLocation reference/input.md 3:1-3:9
- }
- MarkdownReferenceInline {
- value: ""
- reference: [
- MarkdownEmphasisInline {
- value: [
- MarkdownText {
- value: "ipsum"
- loc: SourceLocation reference/input.md 5:3-5:8
- }
- ]
- loc: SourceLocation reference/input.md 5:1-5:10
- }
- ]
- loc: SourceLocation reference/input.md 5:1-5:11
- }
- MarkdownReferenceInline {
- value: ""
- reference: [
- MarkdownEmphasisInline {
- value: [
- MarkdownText {
- value: "lorem"
- loc: SourceLocation reference/input.md 7:3-7:8
- }
- ]
- loc: SourceLocation reference/input.md 7:1-7:10
- }
- ]
- loc: SourceLocation reference/input.md 7:1-7:11
- }
- MarkdownReferenceInline {
- value: ""
- reference: [
- MarkdownEmphasisInline {
- value: [
- MarkdownText {
- value: "lorem"
- loc: SourceLocation reference/input.md 9:2-9:7
- }
- ]
- loc: SourceLocation reference/input.md 9:1-9:8
- }
- ]
- loc: SourceLocation reference/input.md 9:1-9:9
- }
- MarkdownReferenceInline {
- value: ""
- reference: [
- MarkdownText {
- value: "lorem"
- loc: SourceLocation reference/input.md 11:1-11:6
- }
- MarkdownText {
- value: "*"
- loc: SourceLocation reference/input.md 11:6-11:7
- }
- MarkdownText {
- value: " ipsum"
- loc: SourceLocation reference/input.md 11:7-11:13
- }
- MarkdownText {
- value: "**"
- loc: SourceLocation reference/input.md 11:13-11:15
- }
- ]
- loc: SourceLocation reference/input.md 11:1-11:16
- }
- MarkdownReferenceInline {
- value: ""
- reference: [
- MarkdownText {
- value: "*"
- loc: SourceLocation reference/input.md 13:1-13:2
- }
- MarkdownText {
- value: "lorem "
- loc: SourceLocation reference/input.md 13:2-13:8
- }
- MarkdownText {
- value: "**"
- loc: SourceLocation reference/input.md 13:8-13:10
- }
- MarkdownText {
- value: "ipsum"
- loc: SourceLocation reference/input.md 13:10-13:15
- }
- ]
- loc: SourceLocation reference/input.md 13:1-13:16
- }
- ]
- comments: []
- corrupt: false
- diagnostics: []
- path: UIDPath<reference/input.md>
- loc: SourceLocation reference/input.md 1:0-14:0
-}
-```
-
-### `diagnostics`
-
-```
-
-```

fix(markdown-parser, formatter): correctly parse links (#1419)