Do not require space after markdown block language name (#4783)

https://github.com/prettier/prettier/pull/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: https://github.com/prettier/prettier/pull/4153#issuecomment-401174999
This commit is contained in:
Alexander Kachkaev authored and Ika committed 2018-07-02 08:22:56 +08:00
1 parent 3f91d6427d
commit a05fd118be
3 files changed
+20 -1

No files matched your search

+2 -1
View File
@@ -12,7 +12,8 @@ function embed(path, print, textToDoc, options) {
if (node.type === "code" && node.lang !== null) {
// 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/, 1)[0];
const langMatch = node.lang.match(/^[A-Za-z0-9_-]+/);
const lang = langMatch ? langMatch[0] : "";
const parser = getParserName(lang);
if (parser) {
const styleUnit = options.__inJsTemplate ? "~" : "`";
@@ -41,6 +41,12 @@ console.log( "hello world" );
\`\`\`js {cmd=node .line-numbers}
console.log( "hello world" );
\`\`\`
## js block with arguments and no space
\`\`\`js{cmd=node .line-numbers}
console.log( "hello world" );
\`\`\`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## plain js block
@@ -54,6 +60,12 @@ console.log("hello world");
console.log("hello world");
\`\`\`
## js block with arguments and no space
\`\`\`js{cmd=node .line-numbers}
console.log("hello world");
\`\`\`
`;
exports[`trailing-comma.md 1`] = `
@@ -9,3 +9,9 @@ console.log( "hello world" );
```js {cmd=node .line-numbers}
console.log( "hello world" );
```
## js block with arguments and no space
```js{cmd=node .line-numbers}
console.log( "hello world" );
```