asjqkkkk / markdown_widget

📖Rendering markdown by flutter!Welcome for pr and issue.
MIT License
312 stars 90 forks source link

Hi, any possible interface to add a very simple WidgetSpan to markdown widget? #132

Open lucasjinreal opened 11 months ago

lucasjinreal commented 11 months ago

Am using this markdown_widget lib, its extremly powerful and useful.

markdown works like a charm. but now, I have a very very tiny simple demand.

I want detect a certrain parttern, and shows only parts of text as a WidgetSpan.

For exmaple:

image

this is actually markdown widget,

But I want detect this partten, and shows 英翻中 as this: image

is there a way to do it?

asjqkkkk commented 11 months ago

Hi @lucasjinreal , there is already a sample for custom html tag:

https://github.com/asjqkkkk/markdown_widget/blob/master/example/lib/markdown_custom/video.dart

In your case, you need create SpanNodeGeneratorWithTag with cmd tag, then use textGenerator with CustomTextNode

the sample code for cmdGeneratorWithTag

import 'package:flutter/material.dart';
import 'package:markdown_widget/markdown_widget.dart';

class CMDNode extends ElementNode {
  @override
  InlineSpan build() {
    return WidgetSpan(
        child: Container(
      padding: EdgeInsets.all(8),
      decoration: BoxDecoration(
          color: Colors.yellow,
          borderRadius: BorderRadius.all(Radius.circular(4))),
      child: Text.rich(childrenSpan),
    ));
  }
}

SpanNodeGeneratorWithTag cmdGeneratorWithTag = SpanNodeGeneratorWithTag(
    tag: _cmdTag, generator: (e, config, visitor) => CMDNode());

const _cmdTag = 'cmd';

image

lucasjinreal commented 11 months ago

@asjqkkkk hi, seems not work:

 linesMargin: const EdgeInsets.symmetric(horizontal: 4),
                generators: [
                  videoGeneratorWithTag,
                  latexGenerator,
                  cmdGeneratorWithTag,
                ],
                inlineSyntaxList: [LatexSyntax()],
              ).buildWidgets(
                message.text,
                config: isDark
                    ? MarkdownConfig.darkConfig.copy(
                        configs: _getMarkdownConfigs(
                          isDark,
                          bodyTextStyle,
                          codeWrapper,
                        ),
                      )
                    : MarkdownConfig.defaultConfig.copy(
                        configs: _getMarkdownConfigs(
                          isDark,
                          bodyTextStyle,
                          codeWrapper,
                        ),
                      ),
image
asjqkkkk commented 11 months ago

@lucasjinreal you need use textGenerator with CustomTextNode to enable html parser

textGenerator: (node, config, visitor) =>
                          CustomTextNode(node.textContent, config, visitor)

https://github.com/asjqkkkk/markdown_widget/blob/master/example/lib/markdown_custom/custom_node.dart

lucasjinreal commented 11 months ago

I just need will html effect other nodes?

image
MarkdownGenerator(
                linesMargin: const EdgeInsets.symmetric(horizontal: 4),
                generators: [
                  videoGeneratorWithTag,
                  latexGenerator,
                  cmdGeneratorWithTag,
                ],
                inlineSyntaxList: [LatexSyntax()],
                textGenerator: (node, config, visitor) => CMDNode(),
              ).buildWidgets(
                message.text,
                config: isDark
                    ? MarkdownConfig.darkConfig.copy(
                        configs: _getMarkdownConfigs(
                          isDark,
                          bodyTextStyle,
                          codeWrapper,
                        ),
                      )
                    : MarkdownConfig.defaultConfig.copy(
                        configs: _getMarkdownConfigs(
                          isDark,
                          bodyTextStyle,
                          codeWrapper,
                        ),
                      ),
              ),
            ),
asjqkkkk commented 11 months ago

@lucasjinreal CustomTextNode is used to parsing html tags, you should not replace it as CMDNode

lucasjinreal commented 10 months ago

@asjqkkkk hi, the <cmd></cmd> is just I designed for parse a certain pattern, how to make it work without effecting other html parse? On my side, I don't know how to reveal your effect.

asjqkkkk commented 10 months ago

There is the example code:

MarkdownWidget(
                  data: data,
                  markdownGenerator: MarkdownGenerator(
                      generators: [videoGeneratorWithTag, latexGenerator, cmdGeneratorWithTag],
                      inlineSyntaxList: [LatexSyntax()],
                      textGenerator: (node, config, visitor) =>
                          CustomTextNode(node.textContent, config, visitor)),
                )
class CMDNode extends ElementNode {
  @override
  InlineSpan build() {
    return WidgetSpan(
        child: Container(
      padding: EdgeInsets.all(8),
      decoration: BoxDecoration(
          color: Colors.yellow,
          borderRadius: BorderRadius.all(Radius.circular(4))),
      child: Text.rich(childrenSpan),
    ));
  }
}

SpanNodeGeneratorWithTag cmdGeneratorWithTag = SpanNodeGeneratorWithTag(
    tag: _cmdTag, generator: (e, config, visitor) => CMDNode());

const _cmdTag = 'cmd';

image