Closed VertexToEdge closed 3 years ago
with custom syntax
import 'package:charcode/charcode.dart';
import 'ast.dart';
import 'document.dart';
import 'util.dart';
// Parses preformatted code blocks between two ~~~ or ``` sequences.
//
// See the CommonMark spec: https://spec.commonmark.org/0.29/#fenced-code-blocks
class FencedCodeBlockSyntax extends BlockSyntax {
@override
RegExp get pattern => _codeFencePattern;
const FencedCodeBlockSyntax();
@override
bool canParse(BlockParser parser) {
final match = pattern.firstMatch(parser.current);
if (match == null) return false;
final codeFence = match.group(1)!;
final infoString = match.group(2);
// From the CommonMark spec:
//
// > If the info string comes after a backtick fence, it may not contain
// > any backtick characters.
return (codeFence.codeUnitAt(0) != $backquote ||
!infoString!.codeUnits.contains($backquote));
}
@override
List<String> parseChildLines(BlockParser parser, [String? endBlock]) {
endBlock ??= '';
var childLines = <String>[];
parser.advance();
while (!parser.isDone) {
var match = pattern.firstMatch(parser.current);
if (match == null || !match[1]!.startsWith(endBlock)) {
childLines.add(parser.current);
parser.advance();
} else {
parser.advance();
break;
}
}
return childLines;
}
@override
Node parse(BlockParser parser) {
// Get the syntax identifier, if there is one.
var match = pattern.firstMatch(parser.current)!;
var endBlock = match.group(1);
var infoString = match.group(2)!;
var childLines = parseChildLines(parser, endBlock);
// The Markdown tests expect a trailing newline.
childLines.add('');
var text = childLines.join('\n');
if (parser.document.encodeHtml) {
text = escapeHtml(text);
}
var code = Element.text('code', text);
// the info-string should be trimmed
// http://spec.commonmark.org/0.22/#example-100
infoString = infoString.trim();
if (infoString.isNotEmpty) {
// only use the first word in the syntax
// http://spec.commonmark.org/0.22/#example-100
var firstSpace = infoString.indexOf(' ');
if (firstSpace >= 0) {
infoString = infoString.substring(0, firstSpace);
}
if (parser.document.encodeHtml) {
infoString = escapeHtmlAttribute(infoString);
}
code.attributes['class'] = 'language-$infoString';
}
var element = Element('pre', [code]);
return element;
}
}
CodeBlockSyntax
Line Diffing
I didn't use diff method in diff_match_patch. only use Diff class for describing Diff action.
Todo:
Remove diff_match_path dependency
https://pub.dev/packages/diff_match_patch APACHE 2.0 LICENSE
diff method will diff the old text and new text. it return what words deleted and inserted.
it is example.
Output: