Repository navigation
Vue: Support custom blocks - #8023
Conversation
|
I guess we still can't parse this <custom lang="babel">
const foo = "</";
</custom> |
|
@sosukesuzuki prettier/src/language-html/utils.js Line 364 in 5569e33 maybe also both |
a2a451e to
5dc4bb7
Compare
| for (let i = 0; i < parsers.length && !printed; i++) { | ||
| const parser = parsers[i]; | ||
| try { | ||
| printed = textToDoc(node.value, { parser }); |
There was a problem hiding this comment.
I don't like the idea guessing languages, people even complaining about auto format with known language.
There was a problem hiding this comment.
I don't like this too, but in the real world custom blocks are often used without attributes.
There was a problem hiding this comment.
In that way, why don't we ignore it. Want format? Tell me what it is.
There was a problem hiding this comment.
I think formatting for custom blocks is needed. For example, eslint-plugin-gridsome and eslint-plugin-prettier-vue use Prettier to format custom blocks.
Could you tell me what is the reason for ignoring it?
There was a problem hiding this comment.
I'm not saying disable formatting custom blocks.
I'm worried about wrong results.
We should ignore
<docs></docs>But format
<docs lang="markdown"></docs>Maybe with some exception like <page-query> known as graphql block, just like <script> in html we assume that's javascript
There was a problem hiding this comment.
I understood. What do you think about adding options for custom blocks like eslint-plugin-prettier-vue? (I'm reluctant to add options)
There was a problem hiding this comment.
I'll remove guessing languages for now. At least for this PR, it only supports custom blocks using the lang / type attribute.
There was a problem hiding this comment.
👍 , I guess we can use --embedded-language-formatting later?
| function stripTrailingHardline(doc) { | ||
| // HACK remove ending hardline, original PR: #1984 | ||
| if (doc.type === "concat" && doc.parts.length !== 0) { | ||
| const lastPart = doc.parts[doc.parts.length - 1]; | ||
| const parts = getInnerParts(doc); | ||
| const lastPart = parts[parts.length - 1]; | ||
| if (lastPart.type === "concat") { | ||
| if ( | ||
| lastPart.parts.length === 2 && | ||
| lastPart.parts[0].hard && | ||
| lastPart.parts[1].type === "break-parent" | ||
| ) { | ||
| return { type: "concat", parts: doc.parts.slice(0, -1) }; | ||
| return { type: "concat", parts: parts.slice(0, -1) }; |
There was a problem hiding this comment.
Potential breaking change for plugins
There was a problem hiding this comment.
I think we can implement the withInnerParts argument (and maybe put todo to change it on default for prettier@3)
/cc @thorn0
There was a problem hiding this comment.
Are doc-utils considered part of the public plugin API?
c241784 to
6ca2f96
Compare
alexander-akait
left a comment
There was a problem hiding this comment.
Looks good, but want some feedback from @thorn0 about changes in doc-utils
|
Just want let you know, I tried use |
|
Prettier pr-8023 --parser vueInput: <custom lang="zzz">
const foo = "foo";
const foo = "foo";</custom>Output: <custom lang="zzz"> const foo = "foo";
const foo = "foo";>
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
- #8023 (comment)
lang="babel"should not be recognized.lang="javascript"should.babelis not a language, it's Prettier's implementation detail. Check how the language recognition is implemented for code blocks in Markdown.--vue-indent-script-and-styleshouldn't affect custom block whose language wasn't recognized.
|
Prettier pr-8023 --parser vueInput: <custom lang="json">{
"a": 1
}</custom>
<custom lang="json">{
"a": 1
}
</custom>Output: <custom lang="json"
{
"a": 1
}
>
<custom lang="json"
{
"a": 1
}
</custom>
|
There was a problem hiding this comment.
-
Blocks with unknown languages must be preserved as is.
Details
Prettier pr-8023
Playground link--parser vue
Input:
<custom lang="zzz">123</custom>
Output:
<custom lang="zzz"> 123 </custom>
-
Please merge
masteror rebase to see how this will work with--embedded-language-formatting=off.
|
I'm busy this week, so I'll continue working on it over the weekend. |
7d5e818 to
afc96f6
Compare
|
I worked on this because it wasn't as busy as I thought it would be. |
This comment has been minimized.
This comment has been minimized.
|
What about this point? Why can that code be parsed in |
|
@thorn0 Probably, we cannot fix it from Prettier side. angular-html-parser seems to parse |
85d4cec to
ba61e74
Compare
This reverts commit f65c7cd.
1064458 to
d7be06c
Compare
This improves formatting custom blocks in Vue SFC.(see #5448 and #5458)
If there is
langattribute, the value oflangis passed toparseroption.(If the value oflangis unknown, no formatting is done)real world custom blocks examples:
How do you think?
changelog_unreleased/*/pr-XXXX.mdfile followingchangelog_unreleased/TEMPLATE.md.✨Try the playground for this PR✨