Skip to content

Commit

Permalink
feat: implement custom builder for backticks content
Browse files Browse the repository at this point in the history
  • Loading branch information
aman-singh7 committed Mar 21, 2022
1 parent dbdc490 commit effbc42
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
21 changes: 1 addition & 20 deletions lib/services/ib_engine_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,31 +249,12 @@ class IbEngineServiceImpl implements IbEngineService {

if (_ibRawPageData == null) return null;

/// Inline HTML Tags bounded by Backticks(`) are not parsed
/// For example,
/// 2<sup>6</sup> -> 2^6
/// whereas, `2<sup>6</sup>` -> `2<sup>6</sup>`
final content = HtmlUnescape()
.convert(_ibRawPageData.rawContent)
.splitMapJoin(
RegExp(r'\`(.*?)\`'),
onMatch: (m) {
if (m[1] != null &&
RegExp(r'<(\S*?)[^>]*>(.*?)<\/\1>|<.*?\/>').hasMatch(m[1]!)) {
return '${m[1]}';
}

return '${m[0]}';
},
onNonMatch: (n) => n,
);

return IbPageData(
id: _ibRawPageData.id,
pageUrl: _ibRawPageData.httpUrl,
title: _ibRawPageData.title,
content: [
IbMd(content: '$content\n'),
IbMd(content: '${HtmlUnescape().convert(_ibRawPageData.rawContent)}\n'),
],
tableOfContents: _ibRawPageData.hasToc
? _getTableOfContents(_ibRawPageData.content!)
Expand Down
2 changes: 2 additions & 0 deletions lib/ui/views/ib/ib_page_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'package:mobile_app/ui/views/ib/builders/ib_pop_quiz_builder.dart';
import 'package:mobile_app/ui/views/ib/builders/ib_subscript_builder.dart';
import 'package:mobile_app/ui/views/ib/builders/ib_superscript_builder.dart';
import 'package:mobile_app/ui/views/ib/builders/ib_webview_builder.dart';
import 'package:mobile_app/ui/views/ib/syntaxes/ib_backticks_syntax.dart';
import 'package:mobile_app/ui/views/ib/syntaxes/ib_embed_syntax.dart';
import 'package:mobile_app/ui/views/ib/syntaxes/ib_filter_syntax.dart';
import 'package:mobile_app/ui/views/ib/syntaxes/ib_inline_html_syntax.dart';
Expand Down Expand Up @@ -214,6 +215,7 @@ class _IbPageViewState extends State<IbPageView> {
[
IbInlineHtmlSyntax(builders: _inlineBuilders),
IbMathjaxSyntax(),
IbBackTicksSyntax(),
md.EmojiSyntax(),
...md.ExtensionSet.gitHubFlavored.inlineSyntaxes,
],
Expand Down
38 changes: 38 additions & 0 deletions lib/ui/views/ib/syntaxes/ib_backticks_syntax.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'package:markdown/markdown.dart' as md;
import 'package:mobile_app/ui/views/ib/syntaxes/ib_inline_html_syntax.dart';

class IbBackTicksSyntax extends md.InlineSyntax {
IbBackTicksSyntax() : super(_pattern);

static const String _pattern = r'\`(.*?)\`';

@override
bool onMatch(md.InlineParser parser, Match match) {
final String matched = match[1] ?? '';

final inlineRegex = RegExp(IbInlineHtmlSyntax.Pattern);

/// Inline HTML Tags bounded by Backticks(`) are not parsed
if (inlineRegex.hasMatch(matched)) {
for (var word in matched.split(' ')) {
if (inlineRegex.hasMatch(word)) {
parser.addNode(md.Text(word.substring(0, word.indexOf('<'))));

final match = inlineRegex.firstMatch(word);
if (match != null) {
parser.addNode(md.Element.text(match[1]!, match[2]!));
}

parser.addNode(
md.Text("${word.substring(word.lastIndexOf('>') + 1)} "));
continue;
}
parser.addNode(md.Text('$word '));
}
} else {
parser.addNode(md.Element.text('code', matched));
}

return true;
}
}
4 changes: 2 additions & 2 deletions lib/ui/views/ib/syntaxes/ib_inline_html_syntax.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:markdown/markdown.dart' as md;

class IbInlineHtmlSyntax extends md.InlineSyntax {
IbInlineHtmlSyntax({required this.builders}) : super(_pattern);
IbInlineHtmlSyntax({required this.builders}) : super(Pattern);

Map<String, MarkdownElementBuilder> builders;

static const String _pattern = r'<(\S*?)[^>]*>(.*?)<\/\1>|<.*?\/>';
static const String Pattern = r'<(\S*?)[^>]*>(.*?)<\/\1>|<.*?\/>';

@override
bool onMatch(md.InlineParser parser, Match match) {
Expand Down

0 comments on commit effbc42

Please sign in to comment.