-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathdash_image.dart
More file actions
67 lines (56 loc) · 1.84 KB
/
dash_image.dart
File metadata and controls
67 lines (56 loc) · 1.84 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
61
62
63
64
65
66
67
// 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/dom.dart';
import 'package:jaspr/jaspr.dart';
import 'package:jaspr_content/jaspr_content.dart';
import '../../markdown/markdown_parser.dart';
class DashImage with CustomComponentBase {
const DashImage();
@override
Pattern get pattern => RegExp('DashImage', caseSensitive: false);
@override
Component apply(_, Map<String, String> attributes, _) {
final isFigure = attributes.containsKey('figure');
final imgSrc =
attributes['image'] ??
(throw Exception(
'<DashImage> component requires an "image" attribute.',
));
final caption = attributes['caption'] ?? '';
final alt = attributes['alt'] ?? caption;
final figureClass = isFigure ? attributes['class'] : null;
final imgClass = attributes[isFigure ? 'img-class' : 'class'];
final style =
[
?attributes['img-style'],
if (attributes['width'] case final w?) 'width: $w',
if (attributes['height'] case final h?) 'height: $h',
]
.map((style) => style.trim())
.map((style) => style.endsWith(';') ? style : '$style;')
.join(' ');
final child = Component.fragment([
img(
src: '/assets/images/docs/$imgSrc',
alt: alt,
classes: imgClass,
attributes: {
if (style.isNotEmpty) 'style': style,
},
),
if (caption.isNotEmpty)
figcaption(classes: 'figure-caption', [
DashMarkdown(content: caption),
]),
]);
if (isFigure) {
return figure(classes: figureClass, [
div(classes: 'site-figure-container', [
child,
]),
]);
}
return child;
}
}