Skip to content

Commit

Permalink
[flutter_markdown] Fix custom block building used for block syntaxes.
Browse files Browse the repository at this point in the history
* Earlier, `builders` were assumed to be used only with inline syntaxes, therefore builders with BlockSyntax was totally broken
(flutter/flutter#81724)
* Added another named argument, `blockBuilders` specifically for BlockSyntax.
* Preferred TextStyle for custom BlockSyntax will be null. Therefore, it's expected that preferredStyle in MarkdownBlockBuilder is nullable.

Signed-off-by: Manjot Sidhu <[email protected]>
  • Loading branch information
manjotsidhu committed Jun 27, 2021
1 parent 66a6816 commit 29bb87f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
20 changes: 13 additions & 7 deletions packages/flutter_markdown/lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class MarkdownBuilder implements md.NodeVisitor {
required this.checkboxBuilder,
required this.bulletBuilder,
required this.builders,
required this.blockBuilders,
required this.listItemCrossAxisAlignment,
this.fitContent = false,
this.onTapText,
Expand Down Expand Up @@ -129,9 +130,12 @@ class MarkdownBuilder implements md.NodeVisitor {
/// Called when building a custom bullet.
final MarkdownBulletBuilder? bulletBuilder;

/// Call when build a custom widget.
/// Call when build a custom inline widget.
final Map<String, MarkdownElementBuilder> builders;

/// Call when build a custom block widget.
final Map<String, MarkdownElementBuilder> blockBuilders;

/// Whether to allow the widget to fit the child content.
final bool fitContent;

Expand Down Expand Up @@ -188,7 +192,7 @@ class MarkdownBuilder implements md.NodeVisitor {
}

int? start;
if (_isBlockTag(tag)) {
if (_isBlockTag(tag) || blockBuilders.containsKey(tag)) {
_addAnonymousBlockIfNeeded();
if (_isListTag(tag)) {
_listIndents.add(tag);
Expand Down Expand Up @@ -245,10 +249,10 @@ class MarkdownBuilder implements md.NodeVisitor {
element.children!.add(md.Text(''));
}

final TextStyle parentStyle = _inlines.last.style!;
final TextStyle? parentStyle = _inlines.last.style;
_inlines.add(_InlineElement(
tag,
style: parentStyle.merge(styleSheet.styles[tag]),
style: parentStyle?.merge(styleSheet.styles[tag]),
));
}

Expand Down Expand Up @@ -333,7 +337,7 @@ class MarkdownBuilder implements md.NodeVisitor {
void visitElementAfter(md.Element element) {
final String tag = element.tag;

if (_isBlockTag(tag)) {
if (_isBlockTag(tag) || blockBuilders.containsKey(tag)) {
_addAnonymousBlockIfNeeded();

final _BlockElement current = _blocks.removeLast();
Expand Down Expand Up @@ -409,6 +413,8 @@ class MarkdownBuilder implements md.NodeVisitor {
);
} else if (tag == 'hr') {
child = Container(decoration: styleSheet.horizontalRuleDecoration);
} else if (blockBuilders.containsKey(tag)) {
child = blockBuilders[tag]!.visitElementAfter(element, styleSheet.styles[tag])!;
}

_addBlockChild(child);
Expand Down Expand Up @@ -568,7 +574,7 @@ class MarkdownBuilder implements md.NodeVisitor {
if (_inlines.isEmpty) {
_inlines.add(_InlineElement(
tag,
style: styleSheet.styles[tag!],
style: styleSheet.styles.containsKey(tag) ? styleSheet.styles[tag!] : null,
));
}
}
Expand All @@ -589,7 +595,7 @@ class MarkdownBuilder implements md.NodeVisitor {

WrapAlignment blockAlignment = WrapAlignment.start;
TextAlign textAlign = TextAlign.start;
if (_isBlockTag(_currentBlockTag)) {
if (_isBlockTag(_currentBlockTag) || blockBuilders.containsKey(_currentBlockTag)) {
blockAlignment = _wrapAlignmentForBlockTag(_currentBlockTag);
textAlign = _textAlignForBlockTag(_currentBlockTag);
}
Expand Down
20 changes: 19 additions & 1 deletion packages/flutter_markdown/lib/src/widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ abstract class MarkdownWidget extends StatefulWidget {
this.checkboxBuilder,
this.bulletBuilder,
this.builders = const <String, MarkdownElementBuilder>{},
this.blockBuilders = const <String, MarkdownElementBuilder>{},
this.fitContent = false,
this.listItemCrossAxisAlignment =
MarkdownListItemCrossAxisAlignment.baseline,
Expand Down Expand Up @@ -224,7 +225,7 @@ abstract class MarkdownWidget extends StatefulWidget {
/// Called when building a bullet
final MarkdownBulletBuilder? bulletBuilder;

/// Render certain tags, usually used with [extensionSet]
/// Render certain inline tags, usually used with [extensionSet]
///
/// For example, we will add support for `sub` tag:
///
Expand All @@ -237,6 +238,19 @@ abstract class MarkdownWidget extends StatefulWidget {
/// The `SubscriptBuilder` is a subclass of [MarkdownElementBuilder].
final Map<String, MarkdownElementBuilder> builders;

/// Render certain block tags, usually used with custom block syntaxes
///
/// For example, we will add support for `iframe` tag:
///
/// ```dart
/// blockBuilders: {
/// 'iframe': IframeBuilder(),
/// }
/// ```
///
/// The `IframeBuilder` is a subclass of [MarkdownElementBuilder].
final Map<String, MarkdownElementBuilder> blockBuilders;

/// Whether to allow the widget to fit the child content.
final bool fitContent;

Expand Down Expand Up @@ -313,6 +327,7 @@ class _MarkdownWidgetState extends State<MarkdownWidget>
checkboxBuilder: widget.checkboxBuilder,
bulletBuilder: widget.bulletBuilder,
builders: widget.builders,
blockBuilders: widget.blockBuilders,
fitContent: widget.fitContent,
listItemCrossAxisAlignment: widget.listItemCrossAxisAlignment,
onTapText: widget.onTapText,
Expand Down Expand Up @@ -386,6 +401,8 @@ class MarkdownBody extends MarkdownWidget {
MarkdownBulletBuilder? bulletBuilder,
Map<String, MarkdownElementBuilder> builders =
const <String, MarkdownElementBuilder>{},
Map<String, MarkdownElementBuilder> blockBuilders =
const <String, MarkdownElementBuilder>{},
MarkdownListItemCrossAxisAlignment listItemCrossAxisAlignment =
MarkdownListItemCrossAxisAlignment.baseline,
this.shrinkWrap = true,
Expand All @@ -406,6 +423,7 @@ class MarkdownBody extends MarkdownWidget {
imageBuilder: imageBuilder,
checkboxBuilder: checkboxBuilder,
builders: builders,
blockBuilders: blockBuilders,
listItemCrossAxisAlignment: listItemCrossAxisAlignment,
bulletBuilder: bulletBuilder,
fitContent: fitContent,
Expand Down

0 comments on commit 29bb87f

Please sign in to comment.