-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat: support markdown #2943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support markdown #2943
Changes from all commits
d25469f
50aeda5
63de920
41d0f43
f1cce5b
a59dbcc
1ae442a
c8308ee
1e41713
703db97
956eca7
3085364
57d549b
0b42021
9fb9f71
9b85280
032a1c0
fba4662
a982f35
219356e
cecd425
bbd7837
efa96e3
709093f
f764d72
f31025a
9a99ac7
5eead94
a5c64cb
7c15481
2608853
e9d0ed9
9609465
3cb24e7
232dc7f
ec302e6
6b1ed27
78327ad
19c2ae7
1271883
e9c68b1
dc12b70
01ce9ff
0b442c1
066599b
e1ca36d
505fe5a
0e8ecdf
f485b2a
acea7f7
ee546a5
461c97c
0c4b3a7
a1f9761
4f17533
d0630ad
4143c10
654d772
1f4872d
5268a35
f69dad1
4306f43
e57e7d6
05629fe
2653cda
1e4f3ae
ed0e8c1
8a0a90f
aa5e560
7704bb7
f670f1f
506f8bc
f1dcfbe
7c95809
e4a52ac
b8e43cf
65b8e9a
76cc30a
a6a1f05
9e3d01a
90d73b8
0acc85d
2a54bcd
52ca697
a29a73c
1f7a8d1
53d6c95
ea98870
e196a12
d82febe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /node_modules | ||
| npm-debug.log | ||
| *.log | ||
| /errors | ||
| /test.js | ||
| /test.ts | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,59 @@ function getSubtreeParser(path, options) { | |
| case "flow": | ||
| case "typescript": | ||
| return fromBabylonFlowOrTypeScript(path, options); | ||
| case "markdown": | ||
| return fromMarkdown(path, options); | ||
| } | ||
| } | ||
|
|
||
| function fromMarkdown(path, options) { | ||
| const node = path.getValue(); | ||
|
|
||
| if (node.type === "code") { | ||
| const parser = getParserName(node.lang); | ||
| if (parser) { | ||
| const styleUnit = options.__inJsTemplate ? "~" : "`"; | ||
| const style = styleUnit.repeat( | ||
| Math.max(3, util.getMaxContinuousCount(node.value, styleUnit) + 1) | ||
| ); | ||
| return { | ||
| options: { parser }, | ||
| transformDoc: doc => concat([style, node.lang, hardline, doc, style]), | ||
| text: node.value | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
|
|
||
| function getParserName(lang) { | ||
| switch (lang) { | ||
| case "js": | ||
| case "jsx": | ||
| case "javascript": | ||
| return "babylon"; | ||
| case "ts": | ||
| case "tsx": | ||
| case "typescript": | ||
| return "typescript"; | ||
| case "gql": | ||
| case "graphql": | ||
| return "graphql"; | ||
| case "css": | ||
| return "css"; | ||
| case "less": | ||
| return "less"; | ||
| case "scss": | ||
| return "scss"; | ||
| case "json": | ||
| case "json5": | ||
| return "json"; | ||
| case "md": | ||
| case "markdown": | ||
| return "markdown"; | ||
| default: | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -92,11 +145,62 @@ function fromBabylonFlowOrTypeScript(path) { | |
| }; | ||
| } | ||
|
|
||
| /** | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be the first time it is possible to infinitely nest the multiparser...
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. markdown`
```js
markdown`
```js
console.log('hi');
```
`
```
`
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I think we should use
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't even know that existed! |
||
| * md`...` | ||
| * markdown`...` | ||
| */ | ||
| if ( | ||
| parentParent && | ||
| (parentParent.type === "TaggedTemplateExpression" && | ||
| parent.quasis.length === 1 && | ||
| (parentParent.tag.type === "Identifier" && | ||
| (parentParent.tag.name === "md" || | ||
| parentParent.tag.name === "markdown"))) | ||
| ) { | ||
| return { | ||
| options: { parser: "markdown", __inJsTemplate: true }, | ||
| transformDoc: doc => | ||
| concat([ | ||
| indent( | ||
| concat([softline, stripTrailingHardline(escapeBackticks(doc))]) | ||
| ), | ||
| softline | ||
| ]), | ||
| // leading whitespaces matter in markdown | ||
| text: dedent(parent.quasis[0].value.cooked) | ||
| }; | ||
| } | ||
|
|
||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function dedent(str) { | ||
| const spaces = str.match(/\n^( *)/m)[1].length; | ||
| return str.replace(new RegExp(`^ {${spaces}}`, "gm"), "").trim(); | ||
| } | ||
|
|
||
| function escapeBackticks(doc) { | ||
| return util.mapDoc(doc, currentDoc => { | ||
| if (!currentDoc.parts) { | ||
| return currentDoc; | ||
| } | ||
|
|
||
| const parts = []; | ||
|
|
||
| currentDoc.parts.forEach(part => { | ||
| if (typeof part === "string") { | ||
| parts.push(part.replace(/`/g, "\\`")); | ||
| } else { | ||
| parts.push(part); | ||
| } | ||
| }); | ||
|
|
||
| return Object.assign({}, currentDoc, { parts }); | ||
| }); | ||
| } | ||
|
|
||
| function fromHtmlParser2(path, options) { | ||
| const node = path.getValue(); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain why these changes were required?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use for
blockquote(leading>)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/prettier/prettier/pull/2943/files#r142024814