Skip to content

Add MarkdownElementBuilder.wrapBlockWidget to wrap block widgets without replacing them (#124) - #147

Open
dkajiwara wants to merge 1 commit into
foresightmobile:mainfrom
dkajiwara:feat/wrap-block-widget
Open

Add MarkdownElementBuilder.wrapBlockWidget to wrap block widgets without replacing them (#124)#147
dkajiwara wants to merge 1 commit into
foresightmobile:mainfrom
dkajiwara:feat/wrap-block-widget

Conversation

@dkajiwara

Copy link
Copy Markdown

Closes #124

What

Adds MarkdownElementBuilder.wrapBlockWidget(context, element, child).

It is called with a block element's fully built widget and can wrap it while keeping the default rendering. Until now the only hook, visitElementAfterWithContext, replaced the rendering entirely, so attaching a key to a heading meant losing its inline formatting and padding.

With this hook, anchor links work like so (full version in the new Anchor Link Demo):

  1. Register a builder for h1..h6 that wraps each heading in a keyed widget.
  2. In onTapLink, resolve the #fragment to that key and call Scrollable.ensureVisible.

Behavior change

When a block builder's visitText returns null, the default text rendering is now used instead of dropping the text.

…s without replacing them

Returning a widget from visitElementAfterWithContext replaces an element's
default rendering entirely, which makes it impossible to attach a key (or
any other wrapper) to a heading while keeping its inline formatting, style
sheet styles and paddingBuilders padding. That is the building block needed
for scrolling to `#fragment` links (foresightmobile#124).

- Add `wrapBlockWidget(context, element, child)` to MarkdownElementBuilder.
  It is called for block elements with the fully built widget (including
  list bullets and blockquote / code block decoration) and returns `child`
  unchanged by default.
- Fall back to the default text rendering when a block builder's `visitText`
  returns null, so a builder can override only `wrapBlockWidget` and still
  get the element's text rendered.
- Add tests, an anchor link demo to the example app, and a README example.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[flutter_markdown] Relative anchors

1 participant