extractAsSchema method

Map<String, dynamic>? extractAsSchema(
  1. String html,
  2. String schemaType, {
  3. List<StructuredDataType> preferredTypes = const [StructuredDataType.jsonLd, StructuredDataType.microdata, StructuredDataType.rdfa],
})

Extracts structured data and converts it to a specific schema

Implementation

Map<String, dynamic>? extractAsSchema(
  String html,
  String schemaType, {
  List<StructuredDataType> preferredTypes = const [
    StructuredDataType.jsonLd,
    StructuredDataType.microdata,
    StructuredDataType.rdfa,
  ],
}) {
  // Try each preferred type in order
  for (final type in preferredTypes) {
    final results = extractByType(html, type);

    // Find the first result that matches the schema type
    for (final result in results) {
      final resultType = result.data['@type'];
      if (resultType != null) {
        // Check if the type matches (handle both full URLs and short names)
        if (resultType == schemaType ||
            resultType == 'http://schema.org/$schemaType' ||
            resultType == 'https://schema.org/$schemaType' ||
            (resultType is String && resultType.endsWith('/$schemaType'))) {
          return result.data;
        }
      }
    }
  }

  return null;
}