-
Notifications
You must be signed in to change notification settings - Fork 26.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[flutter_markdown] please allow custom block elements #135848
Comments
Hi @sma. Thanks for filing this!
Can you share a completed and minimal reproducible code sample that we can use to reproduce this. |
#127425 has the same reason: An If I add ![]() Regarding my problem, here's a minimal example that work if I add import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
// ignore: depend_on_referenced_packages
import 'package:markdown/markdown.dart' as md;
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Markdown(
data: '# Hallo Welt\n[!NOTE] Dies ist eine wichtige Nachricht.',
// data: 'Foo[^1] bar.\n\n[^1]: Baz.',
blockSyntaxes: const [NoteSyntax()],
builders: {'note': NoteBuilder()},
),
),
);
}
}
class NoteBuilder extends MarkdownElementBuilder {
// super class forgot to declare a const constructor
// const NoteBuilder();
@override
Widget? visitText(md.Text text, TextStyle? preferredStyle) {
return Container(color: Colors.red, child: Text(text.text, style: preferredStyle));
}
}
class NoteSyntax extends md.BlockSyntax {
const NoteSyntax();
@override
md.Node? parse(md.BlockParser parser) {
final line = parser.current;
parser.advance();
return md.Element('note', [md.Text(line.content.substring(8))]);
}
@override
RegExp get pattern => RegExp(r'^\[!NOTE] ');
} |
Hi, you are right about
I can create PR when we find out the best solution. |
/cc @tarrinneal |
Any update on this? Trying to build a custom list block that can use different bullet points per level, but this doesn't seem possible with the way the code is currently structured. Getting the same error where it thinks my custom block element is an inline one. |
Managed to fix this issue for myself by adding an extra parameter to MarkdownBuilder which is a List of Strings that gets added to kBlockTags when _isBlockTag (which I also moved into the class) gets called. Also had to add a call to check the builders for my custom tag in the visitElementAfter method of MarkdownBuilder as there didn't seem to be any way of it doing that on its own. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
Fixes problem with adding custom block syntax. Issue: flutter/flutter#135848
Is there an existing issue for this?
Use case
I tried to add my own custom block element using a
blockSyntaxes: const [NoteSyntax()],
declaration in theMarkdown
widget. MyNoteSyntax
creates anElement('note', children)
which then eventually gets passed to the visitor that creates widgets.I tried to add my own builder using
builders: {'note': NoteBuilder()},
in theMarkdown
widget, but this is never called becauseMarkdownBuilder.visitElementBefore
uses a private function_isBlockTag
to determine whether the providedElement
is a block element by comparing its tag to a hardcoded list of tags.Therefore, it incorrectly thinks my
Element
is an inline element and crashes 1 because_addParentInlineIfNeeded
wants add an inline element that requires atag
but the parent of my block node is the document which has no tag.Proposal
I'd suggest to dispatch the
isBlock
call to the element itself – or let me extend that list.Footnotes
See line 632 of
builder.dart
,style: styleSheet.styles[tag!],
. ↩The text was updated successfully, but these errors were encountered: