Skip to content

Commit e14a52b

Browse files
committed
extract printWord
1 parent fd69335 commit e14a52b

2 files changed

Lines changed: 59 additions & 42 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { PUNCTUATION_REGEXP } from "../constants.evaluate.js";
2+
import { isAutolink } from "../utils.js";
3+
4+
/**
5+
* @import AstPath from "../../common/ast-path.js"
6+
* @import {Doc} from "../../document/index.js"
7+
*/
8+
9+
/**
10+
* @param {AstPath} path
11+
* @return {Doc}
12+
*/
13+
function printWord(path) {
14+
const { node } = path;
15+
let escapedValue = node.value
16+
.replaceAll("*", String.raw`\*`) // escape all `*`
17+
.replaceAll(
18+
new RegExp(
19+
[
20+
`(^|${PUNCTUATION_REGEXP.source})(_+)`,
21+
`(_+)(${PUNCTUATION_REGEXP.source}|$)`,
22+
].join("|"),
23+
"gu",
24+
),
25+
(_, text1, underscore1, underscore2, text2) =>
26+
(underscore1
27+
? `${text1}${underscore1}`
28+
: `${underscore2}${text2}`
29+
).replaceAll("_", String.raw`\_`),
30+
); // escape all `_` except concating with non-punctuation, e.g. `1_2_3` is not considered emphasis
31+
32+
const isFirstSentence = (node, name, index) =>
33+
node.type === "sentence" && index === 0;
34+
const isLastChildAutolink = (node, name, index) =>
35+
isAutolink(node.children[index - 1]);
36+
37+
if (
38+
escapedValue !== node.value &&
39+
(path.match(undefined, isFirstSentence, isLastChildAutolink) ||
40+
path.match(
41+
undefined,
42+
isFirstSentence,
43+
(node, name, index) => node.type === "emphasis" && index === 0,
44+
isLastChildAutolink,
45+
))
46+
) {
47+
// backslash is parsed as part of autolinks, so we need to remove it
48+
escapedValue = escapedValue.replace(/^(\\?[*_])+/u, (prefix) =>
49+
prefix.replaceAll("\\", ""),
50+
);
51+
}
52+
53+
return escapedValue;
54+
}
55+
56+
export { printWord };

src/language-markdown/printer-markdown.js

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ import getMinNotPresentContinuousCount from "../utils/get-min-not-present-contin
1919
import getPreferredQuote from "../utils/get-preferred-quote.js";
2020
import UnexpectedNodeError from "../utils/unexpected-node-error.js";
2121
import clean from "./clean.js";
22-
import { PUNCTUATION_REGEXP } from "./constants.evaluate.js";
2322
import embed from "./embed.js";
2423
import getVisitorKeys from "./get-visitor-keys.js";
2524
import { locEnd, locStart } from "./loc.js";
2625
import { insertPragma } from "./pragma.js";
2726
import { printChildren } from "./print/children.js";
2827
import { printList } from "./print/list.js";
2928
import { printTable } from "./print/table.js";
29+
import { printWord } from "./print/word.js";
3030
import { printParagraph } from "./print-paragraph.js";
3131
import preprocess from "./print-preprocess.js";
3232
import { printSentence } from "./print-sentence.js";
@@ -99,47 +99,8 @@ function genericPrint(path, options, print) {
9999
return printParagraph(path, options, print);
100100
case "sentence":
101101
return printSentence(path, print);
102-
case "word": {
103-
let escapedValue = node.value
104-
.replaceAll("*", String.raw`\*`) // escape all `*`
105-
.replaceAll(
106-
new RegExp(
107-
[
108-
`(^|${PUNCTUATION_REGEXP.source})(_+)`,
109-
`(_+)(${PUNCTUATION_REGEXP.source}|$)`,
110-
].join("|"),
111-
"gu",
112-
),
113-
(_, text1, underscore1, underscore2, text2) =>
114-
(underscore1
115-
? `${text1}${underscore1}`
116-
: `${underscore2}${text2}`
117-
).replaceAll("_", String.raw`\_`),
118-
); // escape all `_` except concating with non-punctuation, e.g. `1_2_3` is not considered emphasis
119-
120-
const isFirstSentence = (node, name, index) =>
121-
node.type === "sentence" && index === 0;
122-
const isLastChildAutolink = (node, name, index) =>
123-
isAutolink(node.children[index - 1]);
124-
125-
if (
126-
escapedValue !== node.value &&
127-
(path.match(undefined, isFirstSentence, isLastChildAutolink) ||
128-
path.match(
129-
undefined,
130-
isFirstSentence,
131-
(node, name, index) => node.type === "emphasis" && index === 0,
132-
isLastChildAutolink,
133-
))
134-
) {
135-
// backslash is parsed as part of autolinks, so we need to remove it
136-
escapedValue = escapedValue.replace(/^(\\?[*_])+/u, (prefix) =>
137-
prefix.replaceAll("\\", ""),
138-
);
139-
}
140-
141-
return escapedValue;
142-
}
102+
case "word":
103+
return printWord(path);
143104
case "whitespace": {
144105
const { next } = path;
145106

0 commit comments

Comments
 (0)