-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathheader_extractor.dart
More file actions
60 lines (49 loc) · 1.53 KB
/
header_extractor.dart
File metadata and controls
60 lines (49 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2025 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:jaspr_content/jaspr_content.dart';
import 'package:meta/meta.dart';
final class HeaderExtractorExtension implements PageExtension {
const HeaderExtractorExtension();
@override
Future<List<Node>> apply(Page page, List<Node> nodes) async {
page.apply(data: {'contentHeaders': _extractHeaders(nodes)});
return nodes;
}
List<ContentHeader> _extractHeaders(List<Node> nodes) {
final headers = <ContentHeader>[];
for (final node in nodes) {
if (node is ElementNode) {
final tagName = node.tag.toLowerCase();
if (tagName.startsWith('h')) {
// Verify this is a header element with a valid level.
final level = int.tryParse(tagName.substring(1));
if (level != null && level >= 1 && level <= 6) {
headers.add(
ContentHeader(
text: node.innerText,
level: level,
attributes: node.attributes,
),
);
}
}
if (node.children case final children?) {
headers.addAll(_extractHeaders(children));
}
}
}
return headers;
}
}
@immutable
final class ContentHeader {
final String text;
final int level;
final Map<String, String> attributes;
const ContentHeader({
required this.text,
required this.level,
this.attributes = const {},
});
}