Skip to main content

json_ld

Function json_ld 

Source
pub fn json_ld(data: &impl Serialize) -> String
Expand description

data as a JSON-LD <script> for a page’s <head>.

The JSON is escaped for HTML: <, > and & become \u003c, \u003e and \u0026 (as do U+2028 and U+2029), so a value containing </script> cannot end the element, and the JSON parses to the same data. In askama: {{ ocre::seo::json_ld(&data)|safe }}.

§Examples

use serde_json::json;

let html = ocre::seo::json_ld(&json!({
    "@context": "https://schema.org",
    "@type": "Organization",
    "name": "PureFrame </script><b>",
}));
assert!(html.starts_with("<script type=\"application/ld+json\">{"));
assert!(html.contains("PureFrame \\u003c/script\\u003e\\u003cb\\u003e"));
assert_eq!(html.matches("</script>").count(), 1);