//! Prettier's `embed.js` and `embed/*`: code of other languages, through the host's [`crate::embed::Embed`]. use std::borrow::Cow; use super::Parser; use super::ast::{Flags, Id, Kind}; use super::print::{Printer, raw_name, replace}; use super::strings; use super::utilities::{ dedent_string, html_split, html_trim_preserve_indentation, is_script_like_tag, unescape_quote_entities, }; use crate::doc::{Align, Doc, Docs}; use crate::embed::{Kind as Purpose, Request}; use crate::{front_matter, languages}; /// Code to hand to the host. pub(crate) struct Embedded<'c> { code: &'c str, language: &'c str, kind: Purpose, /// Indents beyond the printer's own, where the code is printed in an `indent`. extra: usize, } impl<'c> Embedded<'c> { pub(crate) fn request(&self, indents: usize, options: &super::Options<'_>) -> Request<'c> { Request { code: self.code, language: self.language, kind: self.kind, indent: indents + self.extra, width: options.width, } } } fn text(bytes: &[u8]) -> Cow<'_, str> { String::from_utf8_lossy(bytes) } // ───────────────────────────── parsers ───────────────────────────── /// `inferParserByTypeAttribute` fn parser_by_type(kind: &[u8]) -> Option<&'static str> { match kind { b"module" | b"text/javascript" | b"text/babel" | b"text/jsx" | b"application/javascript" => { Some("babel") } b"application/x-typescript" => Some("typescript"), b"text/markdown" => Some("markdown"), b"text/html" => Some("html"), b"text/x-handlebars-template" => Some("glimmer"), _ if kind.ends_with(b"json") || kind.ends_with(b"importmap") || kind == b"speculationrules" => { Some("json") } _ => None, } } fn parser_by_language(language: Option<&[u8]>) -> Option<&'static str> { languages::parser(std::str::from_utf8(language?).ok()?) } /// `inferElementParser` fn element_parser(printer: &Printer<'_, '_>, id: Id) -> Option<&'static str> { let tree = printer.tree; let node = &tree[id]; let attribute = |name: &[u8]| tree.attribute(id, name).map(Option::unwrap_or_default); if &node.name[..] == b"script" && attribute(b"src").is_none() { let (kind, lang) = (attribute(b"type"), attribute(b"lang")); if lang.is_none_or(<[u8]>::is_empty) && kind.is_none_or(<[u8]>::is_empty) { return Some("babel"); } return parser_by_language(lang.filter(|lang| !lang.is_empty())) .or_else(|| parser_by_type(kind.unwrap_or_default())); } if &node.name[..] == b"style" { return match attribute(b"lang").filter(|lang| !lang.is_empty()) { Some(lang) => parser_by_language(Some(lang)), None => Some("css"), }; } if &node.name[..] == b"mj-style" && printer.options.parser == Parser::Mjml { return Some("css"); } if tree.is_vue_non_html_block(id, printer.options) && attribute(b"src").is_none() { let (kind, lang) = (attribute(b"type"), attribute(b"lang")); return parser_by_language(lang.filter(|lang| !lang.is_empty())) .or_else(|| parser_by_type(kind.unwrap_or_default())); } None } // ───────────────────────────── nodes ───────────────────────────── /// A Vue block in another language than HTML. pub(crate) fn element(printer: &mut Printer<'_, '_>, id: Id) -> Option { let tree = printer.tree; let node = &tree[id]; if is_script_like_tag(node, printer.options) || node.has(Flags::IS_SELF_CLOSING) || !tree.is_vue_non_html_block(id, printer.options) { return None; } let language = element_parser(printer, id)?; let tags = super::tag::Tags { tree, options: printer.options }; let content = tags.node_content(id); let mut empty = strings::trim_js_whitespace(content).is_empty(); let mut doc = Docs::EMPTY; if !empty { let code = text(html_trim_preserve_indentation(content)); let formatted = printer.embedded(Embedded { code: &code, language, kind: Purpose::Block, extra: 0 })?; empty = formatted == Docs::EMPTY; doc = formatted; } let prefix = printer.bytes(&marker(&tags, |tags, out| tags.opening_tag_prefix(id, out))); let opening = printer.print_opening_tag(id); let opening = printer.docs.group(opening); let line = if empty { Docs::EMPTY } else { printer.docs.hardline() }; let line_after = if empty { Docs::EMPTY } else { printer.docs.hardline() }; let closing = printer.print_closing_tag(id); let suffix = printer.bytes(&marker(&tags, |tags, out| tags.closing_tag_suffix(id, out))); Some(printer.docs.concat(&[prefix, opening, line, doc, line_after, closing, suffix])) } /// The text in a `