-
Notifications
You must be signed in to change notification settings - Fork 837
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for html underline and videos (#1955)
* fixed #1953 italic detection error fix: issue where when html2md parse <em> to "_" instead of "*" that won't be detected MarkdownToDelta converter feat(test): added test for DeltaX feat: added config classes to MarkdownToDelta and html2md that allow users configure their own styles * Added support for html underline and videos * removed print calls and fix no expect in test * removed useless element attr for underline * improved video url validator pattern * Added support for <video> tag * chore: dart fixes * fix: removed useless params * fix: imports issue --------- Co-authored-by: CatHood0 <[email protected]>
- Loading branch information
Showing
5 changed files
with
136 additions
and
83 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
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,80 @@ | ||
import 'package:html2md/html2md.dart' as hmd; | ||
import 'package:markdown/markdown.dart' as md; | ||
|
||
// [ character | ||
const int _$lbracket = 0x5B; | ||
final RegExp _youtubeVideoUrlValidator = RegExp( | ||
r'^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$'); | ||
|
||
///Local syntax implementation for underline | ||
class UnderlineSyntax extends md.DelimiterSyntax { | ||
UnderlineSyntax() | ||
: super( | ||
'<und>', | ||
requiresDelimiterRun: true, | ||
allowIntraWord: true, | ||
tags: [md.DelimiterTag('u', 5)], | ||
); | ||
} | ||
|
||
class VideoSyntax extends md.LinkSyntax { | ||
VideoSyntax({super.linkResolver}) | ||
: super( | ||
pattern: r'\[', | ||
startCharacter: _$lbracket, | ||
); | ||
|
||
@override | ||
md.Element createNode( | ||
String destination, | ||
String? title, { | ||
required List<md.Node> Function() getChildren, | ||
}) { | ||
final element = md.Element.empty('video'); | ||
element.attributes['src'] = destination; | ||
if (title != null && title.isNotEmpty) { | ||
element.attributes['title'] = title; | ||
} | ||
return element; | ||
} | ||
} | ||
|
||
///This rule avoid the default converter from html2md ignore underline tag for <u> or <ins> | ||
final underlineRule = | ||
hmd.Rule('underline', filters: ['u', 'ins'], replacement: (content, node) { | ||
//Is used a local underline implemenation since markdown just use underline with html tags | ||
return '<und>$content<und>'; | ||
}); | ||
final videoRule = hmd.Rule('video', filters: ['iframe', 'video'], | ||
replacement: (content, node) { | ||
//This need to be verified by a different way of iframes, since video tag can have <source> children | ||
if (node.nodeName == 'video') { | ||
//if has children then just will be taked as different part of code | ||
if (node.childNum > 0) { | ||
var child = node.firstChild!; | ||
var src = child.getAttribute('src'); | ||
if (src == null) { | ||
child = node.childNodes().last; | ||
src = child.getAttribute('src'); | ||
} | ||
if (!_youtubeVideoUrlValidator.hasMatch(src ?? '')) { | ||
return '<video>${child.outerHTML}</video>'; | ||
} | ||
return '[$content]($src)'; | ||
} | ||
final src = node.getAttribute('src'); | ||
if (src == null || !_youtubeVideoUrlValidator.hasMatch(src)) { | ||
return node.outerHTML; | ||
} | ||
return '[$content]($src)'; | ||
} | ||
//by now, we can only access to src | ||
final src = node.getAttribute('src'); | ||
//if the source is null or is not valid youtube url, then just return the html instead remove it | ||
//by now is only available validation for youtube videos | ||
if (src == null || !_youtubeVideoUrlValidator.hasMatch(src)) { | ||
return node.outerHTML; | ||
} | ||
final title = node.getAttribute('title'); | ||
return '[$title]($src)'; | ||
}); |
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 |
---|---|---|
|
@@ -75,6 +75,7 @@ dev_dependencies: | |
yaml: ^3.1.2 | ||
http: ^1.2.1 | ||
|
||
path: any | ||
flutter: | ||
uses-material-design: true | ||
generate: 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