dvApplyPageExtras function

String dvApplyPageExtras(
  1. String html,
  2. DVPageData data
)

html with the page's structured data and favicon in its head. Marked, so rendering the same page again replaces rather than adds.

Implementation

String dvApplyPageExtras(String html, DVPageData data) {
  var out = html.replaceAll(RegExp(r'<!--dv:ld-->.*?<!--/dv:ld-->\n?', dotAll: true), '');
  out = dvApplyFavicon(out, data.favicon);
  final Map<String, Object?>? ld = data.structuredData;
  if (ld != null) {
    // `</` cannot appear inside the script: a value of `</script>` would
    // end it, so the slash is escaped the way JSON allows.
    final String json = jsonEncode(ld).replaceAll('</', r'<\/');
    out = out.replaceFirst('</head>', '<!--dv:ld--><script type="application/ld+json">$json</script><!--/dv:ld-->\n</head>');
  }
  return out;
}