//! 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}; use super::print::{Printer, raw_name}; use super::utilities::{ dedent_string, html_split, html_trim_preserve_indentation, is_script_like_tag, unescape_quote_entities, }; use crate::doc::{Align, Branch}; use crate::embed::{Kind as Purpose, Request}; use crate::strings; 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) -> bool { 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 false; } let Some(language) = element_parser(printer, id) else { return false }; let tags = printer.tags(); let content = tags.node_content(id); let mut formatted = String::new(); if !strings::trim_js_whitespace(content).is_empty() { let code = text(html_trim_preserve_indentation(content)); match printer.embed_text(Embedded { code: &code, language, kind: Purpose::Block, extra: 0 }) { Some(text) => formatted = text, None => return false, } } printer.marker(|tags, out| tags.opening_tag_prefix(id, out)); let group = printer.docs.group(); printer.print_opening_tag(id); printer.docs.close(group); if !formatted.is_empty() { printer.docs.hardline(); printer.text_doc(&formatted); printer.docs.hardline(); } printer.print_closing_tag(id); printer.marker(|tags, out| tags.closing_tag_suffix(id, out)); true } /// The text in a `