Added support for <video> tag

pull/1955/head
CatHood0 10 months ago
parent a19338320c
commit 5ca57b0d66
  1. 25
      lib/src/utils/html2md_utils.dart
  2. 13
      test/utils/delta_x_test.dart

@ -55,8 +55,29 @@ class Html2MdRules {
//Is used a local underline implemenation since markdown just use underline with html tags
return '<und>$content<und>';
});
static final videoRule =
hmd.Rule('video', filters: ['iframe'], replacement: (content, node) {
static 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

@ -9,8 +9,12 @@ void main() {
const htmlWithUnderline =
'<p>This is a normal sentence, and this section has greater <u>underline</u>';
const htmlWithVideo =
const htmlWithIframeVideo =
'<iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ" title="YouTube video player"></iframe>';
const htmlWithVideoTag =
'''<video src="https://www.youtube.com/embed/dQw4w9WgXcQ">Your browser does not support the video tag.</video>
''';
final expectedDeltaEmp = Delta.fromOperations([
Operation.insert(
'This is a normal sentence, and this section has greater emp'),
@ -40,8 +44,13 @@ void main() {
expect(delta, expectedDeltaUnderline);
});
test('should detect iframe and parse correctly', () {
final delta = DeltaX.fromHtml(htmlWithIframeVideo);
expect(delta, expectedDeltaVideo);
});
test('should detect video and parse correctly', () {
final delta = DeltaX.fromHtml(htmlWithVideo);
final delta = DeltaX.fromHtml(htmlWithVideoTag);
expect(delta, expectedDeltaVideo);
});
}

Loading…
Cancel
Save