-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ib): initial beta support for Mathjax forumulas
* This is an initial support for Mathjax forumulas which is implemented using flutter_math_fork. * Formulas which includes {equation} for auto-numbering, {tag} for custom numbering, [ ], $$ $$ multi-line formulas are unsupported by flutter_math_fork. * 75% of the formulas used are working as expected and this initial support should be counted as a baby step towards the stability of future Mathjax formulas. * Flutter_markdown allows either all the selectable widgets in the content or none which means our non-selectable custom Widget which renders Mathjax will break down the support for selectable texts all over Interactive Book. If we enable selectable, Mathjax widget will not render inline like normal paragraph texts instead it will be in a new-line behaving like a block widget with line breaks. (I have won but at what cost Meme here) https://i.redd.it/lwq00qbbna841.png Signed-off-by: Manjot Sidhu <manjot.techie@gmail.com>
- Loading branch information
1 parent
7144fd8
commit 4511344
Showing
6 changed files
with
73 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_markdown/flutter_markdown.dart'; | ||
import 'package:flutter_math_fork/flutter_math.dart'; | ||
import 'package:markdown/markdown.dart' as md; | ||
|
||
/// IbMathjaxBuilder builds RichText widget for the Mathjax Support. | ||
/// | ||
/// It will be inserted/merged inline only if selectable is set to false | ||
/// which means neighbouring widgets should also be RichText so that | ||
/// their children's are merged together otherwise it's rendered | ||
/// as any other block widget with line breaks. | ||
class IbMathjaxBuilder extends MarkdownElementBuilder { | ||
IbMathjaxBuilder(); | ||
|
||
@override | ||
Widget visitElementAfter(md.Element element, TextStyle preferredStyle) { | ||
var _content = element.textContent.trim(); | ||
|
||
return RichText( | ||
text: TextSpan( | ||
children: [ | ||
TextSpan( | ||
// Intentional Nesting due to a bug in flutter_markdown | ||
children: [ | ||
WidgetSpan( | ||
child: SizedBox( | ||
child: Wrap( | ||
spacing: 1.0, | ||
runSpacing: 5.0, | ||
direction: Axis.vertical, | ||
children: <Widget>[Math.tex(_content)], | ||
), | ||
), | ||
), | ||
], | ||
), | ||
], | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import 'package:markdown/markdown.dart' as md; | ||
|
||
class IbMathjaxSyntax extends md.InlineSyntax { | ||
IbMathjaxSyntax() : super(_pattern); | ||
|
||
/// This is not the perfect pattern for any Mathjax formula | ||
/// It's made in accordance to what flutter_math_fork works with | ||
/// [ ], $$ $$ and muti-line formulas are unsupported | ||
static const String _pattern = r'\$([^$\n\r]+)\$'; | ||
|
||
@override | ||
bool onMatch(md.InlineParser parser, Match match) { | ||
if (match[1] != null) { | ||
parser.addNode(md.Element.text('mathjax', match[1])); | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters