Skip to content
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

Support for text highlight. #365

Open
devon opened this issue Apr 2, 2021 · 7 comments
Open

Support for text highlight. #365

devon opened this issue Apr 2, 2021 · 7 comments
Labels
P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug

Comments

@devon
Copy link

devon commented Apr 2, 2021

Some markdown editor like Quilt, iA Writer, Typora support text highlight style:

This is ==highlighted== text.

Any chance we can add the feature?

@wilsonowilson
Copy link

wilsonowilson commented Sep 8, 2021

I'm currently using this to highlight text in my app:

class HighlightSyntax extends TagSyntax {
  HighlightSyntax()
      : super(
          '=+',
          requiresDelimiterRun: true,
          allowIntraWord: true,
        );

  @override
  Node close(
    InlineParser parser,
    Delimiter opener,
    Delimiter closer, {
    required List<Node> Function() getChildren,
  }) {
    return Element('mark', getChildren());
  }
}

@srawlins
Copy link
Member

srawlins commented Sep 8, 2021

Can you link to the output these renderers use? Is @wilsonowilson 's mark tag the correct rendering? Is it standard? I admit I haven't heard of these three tools, but if they're popular or standard in some way, we could support the syntax.

(Note: I'm very happy that TagSyntax seems to be extensible enough to cover this. 😁 )

@wilsonowilson
Copy link

wilsonowilson commented Sep 8, 2021

There is no CommonMark standard, but plugins for other languages like markdown-it like: https://github.com/markdown-it/markdown-it-mark use the same tag & delimiter. Apps like AI Writer and Bear export to html using this tag as well.

See https://stackoverflow.com/questions/25104738/text-highlight-in-markdown for more info.

Since there is no standard, the delimiter varies between apps, but the most popular, used by Obsidian.md, Typora, Ai Writer, Quilt, and Nota.md is ==. I've only seen :: in Bear and ^^ in Roam

@kateile
Copy link

kateile commented Jan 25, 2022

@wilsonowilson thanks for that workaround. However it doesn't work as expected in my app.

Here is how I use it

import 'package:flutter/material.dart' hide Element;
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:markdown/markdown.dart' hide Text;
import 'package:markdown/markdown.dart' as md;

/// Your code exactly
class HighlightSyntax extends TagSyntax {
  HighlightSyntax()
      : super(
          '=+',
          requiresDelimiterRun: true,
          allowIntraWord: true,
        );

  @override
  Node close(
    InlineParser parser,
    Delimiter opener,
    Delimiter closer, {
    required List<Node> Function() getChildren,
  }) {
    return Element('mark', getChildren());
  }
}

/// mark element builder
class HighlightElementBuilder extends MarkdownElementBuilder {
  @override
  Widget? visitElementAfter(md.Element element, preferredStyle) {
    return Text(
      element.textContent,
      style: const TextStyle(
        backgroundColor: Colors.yellow,
        fontWeight: FontWeight.bold,
      ),
    );
  }
}

and in widget I use it as

///This is nested in widget
Markdown(
        data: body,
        inlineSyntaxes: [HighlightSyntax()],
        builders: {
          'mark': HighlightElementBuilder(),
        },
      ),

When I try parsing the following data

# 99.9 Test MD file

==This should be highlighted.==

This should not be highlighted.

it is rendered as
111111111111

as you can see some of words are duplicates.
May you help me what I am doing wrong?

@srawlins srawlins added P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug labels Feb 17, 2022
@srawlins
Copy link
Member

In #407 @chenzhiguang is changing TagSyntax into DelimiterSyntax which should allow developers to implement a highlight syntax, easier than the current TagSyntax.

Since text highlighting syntax is not currently a standard syntax, or part of a major flavor (like GitHub-flavored Markdown), I don't think we will implement or maintain it in this base package. The new DelimiterSyntax should allow a developer to make their own markdown extension package which implements such extensions.

@chenzhiguang
Copy link
Contributor

Here is an example of implementing a highlight syntax:

final result = markdownToHtml('==highlight==', inlineSyntaxes: [
  DelimiterSyntax('=+',
      requiresDelimiterRun: true,
      allowIntraWord: true,
      tags: [DelimiterTag('mark', 2)]),
]);

or

class HighlightSyntax extends DelimiterSyntax {
  HighlightSyntax()
      : super('=+',
            requiresDelimiterRun: true,
            allowIntraWord: true,
            tags: [DelimiterTag('mark', 2)]);
}

final result = markdownToHtml('==highlight==', inlineSyntaxes: [
  HighlightSyntax(),
]);

@Balin-P
Copy link

Balin-P commented Oct 25, 2023

@wilsonowilson thanks for that workaround. However it doesn't work as expected in my app.

Here is how I use it

import 'package:flutter/material.dart' hide Element;
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:markdown/markdown.dart' hide Text;
import 'package:markdown/markdown.dart' as md;

/// Your code exactly
class HighlightSyntax extends TagSyntax {
  HighlightSyntax()
      : super(
          '=+',
          requiresDelimiterRun: true,
          allowIntraWord: true,
        );

  @override
  Node close(
    InlineParser parser,
    Delimiter opener,
    Delimiter closer, {
    required List<Node> Function() getChildren,
  }) {
    return Element('mark', getChildren());
  }
}

/// mark element builder
class HighlightElementBuilder extends MarkdownElementBuilder {
  @override
  Widget? visitElementAfter(md.Element element, preferredStyle) {
    return Text(
      element.textContent,
      style: const TextStyle(
        backgroundColor: Colors.yellow,
        fontWeight: FontWeight.bold,
      ),
    );
  }
}

and in widget I use it as

///This is nested in widget
Markdown(
        data: body,
        inlineSyntaxes: [HighlightSyntax()],
        builders: {
          'mark': HighlightElementBuilder(),
        },
      ),

When I try parsing the following data

# 99.9 Test MD file

==This should be highlighted.==

This should not be highlighted.

it is rendered as 111111111111

as you can see some of words are duplicates. May you help me what I am doing wrong?

Was running into the same problem when trying to implement this. It seems to be something to do with having an empty line following the highlighted text, appears to be some kind of bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

6 participants