fix: removed useless params

pull/1955/head
CatHood0 10 months ago
parent 6ecc86cb32
commit e54277d06a
  1. 68
      lib/src/models/documents/delta_x.dart
  2. 84
      lib/src/utils/delta_x_utils.dart
  3. 126
      lib/src/utils/html2md_utils.dart

@ -3,7 +3,7 @@ import 'package:markdown/markdown.dart' as md;
import 'package:meta/meta.dart';
import '../../../markdown_quill.dart';
import '../../../quill_delta.dart';
import '../../utils/html2md_utils.dart';
import '../../utils/delta_x_utils.dart';
@immutable
@experimental
@ -15,21 +15,12 @@ class DeltaX {
/// This api is **experimental** and designed to be used **internally** and shouldn't
/// used for **production applications**.
@experimental
static Delta fromMarkdown(
String markdownText, {
Md2DeltaConfigs md2DeltaConfigs = const Md2DeltaConfigs(),
}) {
static Delta fromMarkdown(String markdownText) {
final mdDocument = md.Document(
encodeHtml: false, inlineSyntaxes: [UnderlineSyntax(), VideoSyntax()]);
final mdToDelta = MarkdownToDelta(
markdownDocument: mdDocument,
customElementToBlockAttribute:
md2DeltaConfigs.customElementToBlockAttribute,
customElementToEmbeddable: md2DeltaConfigs.customElementToEmbeddable,
customElementToInlineAttribute:
md2DeltaConfigs.customElementToInlineAttribute,
softLineBreak: md2DeltaConfigs.softLineBreak,
encodeHtml: false,
inlineSyntaxes: [UnderlineSyntax(), VideoSyntax()],
);
final mdToDelta = MarkdownToDelta(markdownDocument: mdDocument);
return mdToDelta.convert(markdownText);
}
@ -44,48 +35,15 @@ class DeltaX {
/// used for **production applications**.
///
@experimental
static Delta fromHtml(
String htmlText, {
Html2MdConfigs? configs,
}) {
configs = Html2MdConfigs(
customRules: [
Html2MdRules.underlineRule,
Html2MdRules.videoRule,
...configs?.customRules ?? []
],
ignoreIf: configs?.ignoreIf,
rootTag: configs?.rootTag,
imageBaseUrl: configs?.imageBaseUrl,
styleOptions: configs?.styleOptions ?? {'emDelimiter': '*'},
static Delta fromHtml(String htmlText) {
final markdownText = html2md.convert(
htmlText,
rules: [underlineRule, videoRule],
styleOptions: {'emDelimiter': '*'},
).replaceAll(
'unsafe:',
'',
);
final markdownText = html2md
.convert(
htmlText,
rules: configs.customRules,
ignore: configs.ignoreIf,
rootTag: configs.rootTag,
imageBaseUrl: configs.imageBaseUrl,
styleOptions: configs.styleOptions,
)
.replaceAll(
'unsafe:',
'',
);
return fromMarkdown(markdownText);
}
}
@experimental
class Md2DeltaConfigs {
const Md2DeltaConfigs({
this.customElementToInlineAttribute = const {},
this.customElementToBlockAttribute = const {},
this.customElementToEmbeddable = const {},
this.softLineBreak = false,
});
final Map<String, ElementToAttributeConvertor> customElementToInlineAttribute;
final Map<String, ElementToAttributeConvertor> customElementToBlockAttribute;
final Map<String, ElementToEmbeddableConvertor> customElementToEmbeddable;
final bool softLineBreak;
}

@ -0,0 +1,84 @@
import 'package:html2md/html2md.dart' as hmd;
import 'package:markdown/markdown.dart' as md;
import 'package:markdown/src/ast.dart' as ast;
import 'package:markdown/src/util.dart' as util;
// [ 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
ast.Element createNode(
String destination,
String? title, {
required List<ast.Node> Function() getChildren,
}) {
final element = md.Element.empty('video');
element.attributes['src'] = util.normalizeLinkDestination(
util.escapePunctuation(destination),
);
if (title != null && title.isNotEmpty) {
element.attributes['title'] = util.normalizeLinkTitle(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)';
});

@ -1,126 +0,0 @@
// ignore_for_file: implementation_imports
import 'package:html2md/html2md.dart' as hmd;
import 'package:markdown/markdown.dart' as md;
import 'package:markdown/src/ast.dart' as ast;
import 'package:markdown/src/util.dart' as util;
import 'package:meta/meta.dart';
// [ 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
ast.Element createNode(
String destination,
String? title, {
required List<ast.Node> Function() getChildren,
}) {
final element = md.Element.empty('video');
element.attributes['src'] = util.normalizeLinkDestination(
util.escapePunctuation(destination),
);
if (title != null && title.isNotEmpty) {
element.attributes['title'] = util.normalizeLinkTitle(title);
}
return element;
}
}
class Html2MdRules {
const Html2MdRules._();
///This rule avoid the default converter from html2md ignore underline tag for <u> or <ins>
static 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>';
});
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
//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)';
});
}
@experimental
class Html2MdConfigs {
const Html2MdConfigs({
this.customRules,
this.ignoreIf,
this.rootTag,
this.imageBaseUrl,
this.styleOptions,
});
/// The [rules] parameter can be used to customize element processing.
final List<hmd.Rule>? customRules;
/// Elements list in [ignore] would be ingored.
final List<String>? ignoreIf;
final String? rootTag;
final String? imageBaseUrl;
/// The default and available style options:
///
/// | Name | Default | Options |
/// | ------------- |:-------------:| -----:|
/// | headingStyle | "setext" | "setext", "atx" |
/// | hr | "* * *" | "* * *", "- - -", "_ _ _" |
/// | bulletListMarker | "*" | "*", "-", "_" |
/// | codeBlockStyle | "indented" | "indented", "fenced" |
/// | fence | "\`\`\`" | "\`\`\`", "~~~" |
/// | emDelimiter | "_" | "_", "*" |
/// | strongDelimiter | "**" | "**", "__" |
/// | linkStyle | "inlined" | "inlined", "referenced" |
/// | linkReferenceStyle | "full" | "full", "collapsed", "shortcut" |
final Map<String, String>? styleOptions;
}
Loading…
Cancel
Save