extractOpenGraph static method

OpenGraphData? extractOpenGraph(
  1. String html
)

Extract Open Graph metadata

Implementation

static OpenGraphData? extractOpenGraph(String html) {
  String? getOgContent(String property) {
    final pattern =
        '<meta[^>]*property=[\"\x27]og:$property[\"\x27][^>]*content=[\"\x27]([^\"\x27]*)[\"\x27]';
    final match = RegExp(pattern, caseSensitive: false).firstMatch(html);
    return match?.group(1);
  }

  final title = getOgContent('title');
  final description = getOgContent('description');
  final image = getOgContent('image');
  final url = getOgContent('url');
  final type = getOgContent('type');
  final siteName = getOgContent('site_name');

  if (title != null || description != null || image != null) {
    return OpenGraphData(
      title: title,
      description: description,
      image: image,
      url: url,
      type: type,
      siteName: siteName,
    );
  }

  return null;
}