Skip to content

Commit

Permalink
support JSON in <script> tag
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Feb 19, 2024
1 parent 939edd5 commit 90c2b4b
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 18 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ dprint config add g-plane/malva
dprint config add typescript
```

If you also want to format JSON in `<script>` tag whose `"type"` is `"importmap"` or `"application/json"`,
you can add dprint-plugin-json:

```bash
dprint config add json
```

Or Biome:

```diff
- dprint config add typescript
- dprint config add json
+ dprint config add biome
```

After adding the dprint plugins, update your `dprint.json` and add configuration:

```jsonc
Expand Down
1 change: 1 addition & 0 deletions dprint_plugin/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fn integration_with_dprint_ts_snapshot() {
.arg("../target/wasm32-unknown-unknown/debug/dprint_plugin_markup.wasm")
.arg("https://plugins.dprint.dev/g-plane/malva-v0.1.4.wasm")
.arg("https://plugins.dprint.dev/typescript-0.88.9.wasm")
.arg("https://plugins.dprint.dev/json-0.19.1.wasm")
.stdin(file)
.output()
.unwrap()
Expand Down
19 changes: 19 additions & 0 deletions dprint_plugin/tests/integration/biome/json.html.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
source: dprint_plugin/tests/integration.rs
---
<html>
<body>
<script type="importmap">
{
"imports": {
"square": "./module/shapes/square.js",
"circle": "https://example.com/shapes/circle.js"
}
}
</script>
<script type="application/json">
{ "ssrData": { "items": [1, 2, 3] } }
</script>
</body>
</html>

19 changes: 19 additions & 0 deletions dprint_plugin/tests/integration/dprint_ts/json.html.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
source: dprint_plugin/tests/integration.rs
---
<html>
<body>
<script type="importmap">
{
"imports": {
"square": "./module/shapes/square.js",
"circle": "https://example.com/shapes/circle.js"
}
}
</script>
<script type="application/json">
{ "ssrData": { "items": [1, 2, 3] } }
</script>
</body>
</html>

10 changes: 10 additions & 0 deletions dprint_plugin/tests/integration/json.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<body>
<script type=importmap>
{"imports": { "square": "./module/shapes/square.js", "circle": "https://example.com/shapes/circle.js" }}
</script>
<script type="application/json">
{"ssrData":{"items":[1,2,3]}}
</script>
</body>
</html>
14 changes: 14 additions & 0 deletions markup_fmt/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,20 @@ where
)
}

pub(crate) fn format_json<'a>(&mut self, code: &'a str) -> Cow<'a, str> {
self.format_with_external_formatter(
Path::new("code.json"),
code,
self.print_width
.saturating_sub(self.indent_level)
.saturating_sub(if self.script_indent() {
self.indent_width
} else {
0
}),
)
}

fn format_with_external_formatter<'a>(
&mut self,
path: &Path,
Expand Down
51 changes: 33 additions & 18 deletions markup_fmt/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,24 +322,39 @@ impl<'s> DocGen<'s> for Element<'s> {
let is_root = mem::replace(&mut ctx.is_root, false);
if tag_name.eq_ignore_ascii_case("script") {
if let [Node::TextNode(text_node)] = &self.children[..] {
let formatted = ctx.format_script(
text_node.raw,
self.attrs
.iter()
.find_map(|attr| match attr {
Attribute::NativeAttribute(native_attribute)
if native_attribute.name.eq_ignore_ascii_case("lang") =>
{
native_attribute.value
}
_ => None,
})
.unwrap_or(if matches!(ctx.language, Language::Astro) {
"ts"
} else {
"js"
}),
);
let is_json = self.attrs.iter().any(|attr| {
if let Attribute::NativeAttribute(native_attr) = attr {
native_attr.name.eq_ignore_ascii_case("type")
&& native_attr
.value
.map(|value| value == "importmap" || value == "application/json")
.unwrap_or_default()
} else {
false
}
});
let formatted = if is_json {
ctx.format_json(text_node.raw)
} else {
ctx.format_script(
text_node.raw,
self.attrs
.iter()
.find_map(|attr| match attr {
Attribute::NativeAttribute(native_attribute)
if native_attribute.name.eq_ignore_ascii_case("lang") =>
{
native_attribute.value
}
_ => None,
})
.unwrap_or(if matches!(ctx.language, Language::Astro) {
"ts"
} else {
"js"
}),
)
};
let doc = Doc::hard_line().concat(reflow_with_indent(formatted.trim()));
docs.push(
if ctx.script_indent() {
Expand Down

0 comments on commit 90c2b4b

Please sign in to comment.