-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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(markdown): support fenced codeblock lang followed by attributes #4153
feat(markdown): support fenced codeblock lang followed by attributes #4153
Conversation
src/language-markdown/embed.js
Outdated
@@ -13,7 +13,9 @@ function embed(path, print, textToDoc, options) { | |||
const node = path.getValue(); | |||
|
|||
if (node.type === "code") { | |||
const parser = getParserName(node.lang); | |||
// only look for the first string so as to support [markdown-preview-enhanced](https://shd101wyy.github.io/markdown-preview-enhanced/#/code-chunk) | |||
const lang = node.lang.split(/\s/)[0]; |
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.
How about adding limit
to split
?
const lang = node.lang.split(/\s/, 1)[0];
We only use [0]
so there is no point in extracting all words from the string
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.
Sure 👍
@ikatyang sorry for a bit of off-topic, but since we're got code embedding here, do you know how a language parser can know that what it's formatting is a part of markdown, not a standalone file? Is there any flag getting passed down the tree that could help detect that? Just asked about this on gitter 🤔 |
Should be prettier/src/main/multiparser.js Line 21 in e237320
|
Thank you for this hint @ikatyang! Indeed, function parse(text, parsers, opts) {
const isCodeBlockInMarkdown = opts.parentParser === 'markdown';
// ...
} Really looking forward to see the result of this PR on npm! 😍 When about is Prettier team planning to cut a new release? |
@ikatyang could you recall a reason why we decided not to support Atom turns on syntax highlighting for code blocks even without spaces, which makes things a bit confusing four us in litvis: When it is visible that the code's language has been detected, there is an expectation that it will be formatted by Prettier too, but this does not happen. If I change the regexp to something like |
GitHub does not support it:
I won't object such change, though I think whitespace is more non-controversial. |
#4153 made it possible to detect fenced code block language when it is followed by arguments (e.g. ` ```js {something=something} `). This PR makes it also possible to detect language in cases cases like ` ```js{something=something} ` (no space). The reason for this change is that Atom highlights code blocks regardless of a space after the language name, which makes users wonder why the correctly detected code block is not being formatted: <img width="324" alt="screen shot 2018-06-28 at 22 08 13" src="https://user-images.githubusercontent.com/608862/42060780-db11f5b6-7b1f-11e8-9f43-fe91843f7d89.png"> PR background: #4153 (comment)
Fixes #4150
```js {something=something}
```js{something=something}