| 1 | function embed(path /* , options*/) { |
| 2 | const { node } = path; |
| 3 | |
| 4 | if (node.type !== "TextNode") { |
| 5 | return; |
| 6 | } |
| 7 | |
| 8 | const { parent } = path; |
| 9 | |
| 10 | if (!( |
| 11 | parent.type === "ElementNode" && |
| 12 | parent.tag === "style" && |
| 13 | parent.children.length === 1 && |
| 14 | parent.children[0] === node |
| 15 | )) { |
| 16 | return; |
| 17 | } |
| 18 | |
| 19 | const languageAttribute = parent.attributes.find( |
| 20 | (attribute) => attribute.type === "AttrNode" && attribute.name === "lang", |
| 21 | ); |
| 22 | if ( |
| 23 | languageAttribute && |
| 24 | !( |
| 25 | languageAttribute.value.type === "TextNode" && |
| 26 | (languageAttribute.value.chars === "" || |
| 27 | languageAttribute.value.chars === "css") |
| 28 | ) |
| 29 | ) { |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | return async (textToDoc) => { |
| 34 | const context = node.chars; |
| 35 | if (!context.trim()) { |
| 36 | return ""; |
| 37 | } |
| 38 | const doc = await textToDoc(context, { parser: "css" }); |
| 39 | return doc; |
| 40 | }; |
| 41 | } |
| 42 | |
| 43 | export default embed; |