Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

Commit 8bb9062

Browse files
authored
Add trailing commas to some parameter lists to get better formatting (#420)
1 parent 1c5f2e7 commit 8bb9062

13 files changed

+424
-235
lines changed

bin/markdown.dart

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@ Future<void> main(List<String> args) async {
1919
final parser = ArgParser()
2020
..addFlag('help', negatable: false, help: 'Print help text and exit')
2121
..addFlag('version', negatable: false, help: 'Print version and exit')
22-
..addOption('extension-set',
23-
allowed: ['none', 'CommonMark', 'GitHubFlavored', 'GitHubWeb'],
24-
defaultsTo: 'CommonMark',
25-
help: 'Specify a set of extensions',
26-
allowedHelp: {
27-
'none': 'No extensions; similar to Markdown.pl',
28-
'CommonMark': 'Parse like CommonMark Markdown (default)',
29-
'GitHubFlavored': 'Parse like GitHub Flavored Markdown',
30-
'GitHubWeb': 'Parse like GitHub\'s Markdown-enabled web input fields',
31-
});
22+
..addOption(
23+
'extension-set',
24+
allowed: ['none', 'CommonMark', 'GitHubFlavored', 'GitHubWeb'],
25+
defaultsTo: 'CommonMark',
26+
help: 'Specify a set of extensions',
27+
allowedHelp: {
28+
'none': 'No extensions; similar to Markdown.pl',
29+
'CommonMark': 'Parse like CommonMark Markdown (default)',
30+
'GitHubFlavored': 'Parse like GitHub Flavored Markdown',
31+
'GitHubWeb': 'Parse like GitHub\'s Markdown-enabled web input fields',
32+
},
33+
);
3234
final results = parser.parse(args);
3335

3436
if (results['help'] as bool) {

example/app.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ void main() {
6262
void _renderMarkdown([Event? event]) {
6363
final markdown = markdownInput.value!;
6464

65-
htmlDiv.setInnerHtml(md.markdownToHtml(markdown, extensionSet: extensionSet),
66-
treeSanitizer: nullSanitizer);
65+
htmlDiv.setInnerHtml(
66+
md.markdownToHtml(markdown, extensionSet: extensionSet),
67+
treeSanitizer: nullSanitizer,
68+
);
6769

6870
for (final block in htmlDiv.querySelectorAll('pre code')) {
6971
try {

lib/src/block_parser.dart

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,10 @@ class TableSyntax extends BlockSyntax {
10071007
/// [alignments] is used to annotate an alignment on each cell, and
10081008
/// [cellType] is used to declare either "td" or "th" cells.
10091009
Element _parseRow(
1010-
BlockParser parser, List<String?> alignments, String cellType) {
1010+
BlockParser parser,
1011+
List<String?> alignments,
1012+
String cellType,
1013+
) {
10111014
final line = parser.current;
10121015
final cells = <String>[];
10131016
var index = _walkPastOpeningPipe(line);
@@ -1152,7 +1155,9 @@ class ParagraphSyntax extends BlockSyntax {
11521155
/// Extract reference link definitions from the front of the paragraph, and
11531156
/// return the remaining paragraph lines.
11541157
List<String>? _extractReflinkDefinitions(
1155-
BlockParser parser, List<String> lines) {
1158+
BlockParser parser,
1159+
List<String> lines,
1160+
) {
11561161
bool lineStartsReflinkDefinition(int i) =>
11571162
lines[i].startsWith(_reflinkDefinitionStart);
11581163

@@ -1237,13 +1242,14 @@ class ParagraphSyntax extends BlockSyntax {
12371242
// Returns whether [contents] could be parsed as a reference link definition.
12381243
bool _parseReflinkDefinition(BlockParser parser, String contents) {
12391244
final pattern = RegExp(
1240-
// Leading indentation.
1241-
'''^[ ]{0,3}'''
1242-
// Reference id in brackets, and URL.
1243-
r'''\[((?:\\\]|[^\]])+)\]:\s*(?:<(\S+)>|(\S+))\s*'''
1244-
// Title in double or single quotes, or parens.
1245-
r'''("[^"]+"|'[^']+'|\([^)]+\)|)\s*$''',
1246-
multiLine: true);
1245+
// Leading indentation.
1246+
'''^[ ]{0,3}'''
1247+
// Reference id in brackets, and URL.
1248+
r'''\[((?:\\\]|[^\]])+)\]:\s*(?:<(\S+)>|(\S+))\s*'''
1249+
// Title in double or single quotes, or parens.
1250+
r'''("[^"]+"|'[^']+'|\([^)]+\)|)\s*$''',
1251+
multiLine: true,
1252+
);
12471253
final match = pattern.firstMatch(contents);
12481254
if (match == null) {
12491255
// Not a reference link definition.

lib/src/inline_parser.dart

Lines changed: 120 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,23 @@ class InlineParser {
232232
final openerTextNodeIndex = _tree.indexOf(openerTextNode);
233233
final closerTextNode = closer.node;
234234
var closerTextNodeIndex = _tree.indexOf(closerTextNode);
235-
final node = opener.syntax.close(this, opener, closer,
236-
tag: matchedTag.tag,
237-
getChildren: () =>
238-
_tree.sublist(openerTextNodeIndex + 1, closerTextNodeIndex));
235+
final node = opener.syntax.close(
236+
this,
237+
opener,
238+
closer,
239+
tag: matchedTag.tag,
240+
getChildren: () => _tree.sublist(
241+
openerTextNodeIndex + 1,
242+
closerTextNodeIndex,
243+
),
244+
);
239245
// Replace all of the nodes between the opener and the closer (which
240246
// are now the new emphasis node's children) with the emphasis node.
241247
_tree.replaceRange(
242-
openerTextNodeIndex + 1, closerTextNodeIndex, [node!]);
248+
openerTextNodeIndex + 1,
249+
closerTextNodeIndex,
250+
[node!],
251+
);
243252
// Slide [closerTextNodeIndex] back accordingly.
244253
closerTextNodeIndex = openerTextNodeIndex + 2;
245254

@@ -483,8 +492,10 @@ class EscapeSyntax extends InlineSyntax {
483492
/// Markdown benchmarking is more mature.
484493
class InlineHtmlSyntax extends TextSyntax {
485494
InlineHtmlSyntax()
486-
: super(r'<[/!?]?[A-Za-z][A-Za-z0-9-]*(?:\s[^>]*)?>',
487-
startCharacter: $lt);
495+
: super(
496+
r'<[/!?]?[A-Za-z][A-Za-z0-9-]*(?:\s[^>]*)?>',
497+
startCharacter: $lt,
498+
);
488499
}
489500

490501
/// Matches autolinks like `<[email protected]>`.
@@ -716,15 +727,15 @@ class SimpleDelimiter implements Delimiter {
716727

717728
final int endPos;
718729

719-
SimpleDelimiter(
720-
{required this.node,
721-
required this.char,
722-
required this.length,
723-
required this.canOpen,
724-
required this.canClose,
725-
required this.syntax,
726-
required this.endPos})
727-
: isActive = true;
730+
SimpleDelimiter({
731+
required this.node,
732+
required this.char,
733+
required this.length,
734+
required this.canOpen,
735+
required this.canClose,
736+
required this.syntax,
737+
required this.endPos,
738+
}) : isActive = true;
728739
}
729740

730741
/// An implementation of [Delimiter] which uses concepts of "left-flanking" and
@@ -811,11 +822,15 @@ class DelimiterRun implements Delimiter {
811822

812823
/// Tries to parse a delimiter run from [runStart] (inclusive) to [runEnd]
813824
/// (exclusive).
814-
static DelimiterRun? tryParse(InlineParser parser, int runStart, int runEnd,
815-
{required DelimiterSyntax syntax,
816-
required List<DelimiterTag> tags,
817-
required Text node,
818-
bool allowIntraWord = false}) {
825+
static DelimiterRun? tryParse(
826+
InlineParser parser,
827+
int runStart,
828+
int runEnd, {
829+
required DelimiterSyntax syntax,
830+
required List<DelimiterTag> tags,
831+
required Text node,
832+
bool allowIntraWord = false,
833+
}) {
819834
bool leftFlanking,
820835
rightFlanking,
821836
precededByPunctuation,
@@ -911,12 +926,13 @@ class DelimiterSyntax extends InlineSyntax {
911926
/// is passed, this syntax parses according to the same nesting rules as
912927
/// emphasis delimiters. If [startCharacter] is passed, it is used as a
913928
/// pre-matching check which is faster than matching against [pattern].
914-
DelimiterSyntax(String pattern,
915-
{this.requiresDelimiterRun = false,
916-
int? startCharacter,
917-
this.allowIntraWord = false,
918-
this.tags})
919-
: super(pattern, startCharacter: startCharacter);
929+
DelimiterSyntax(
930+
String pattern, {
931+
this.requiresDelimiterRun = false,
932+
int? startCharacter,
933+
this.allowIntraWord = false,
934+
this.tags,
935+
}) : super(pattern, startCharacter: startCharacter);
920936

921937
@override
922938
bool onMatch(InlineParser parser, Match match) {
@@ -926,22 +942,27 @@ class DelimiterSyntax extends InlineSyntax {
926942
final text = Text(parser.source.substring(matchStart, matchEnd));
927943
if (!requiresDelimiterRun) {
928944
parser._pushDelimiter(SimpleDelimiter(
929-
node: text,
930-
length: runLength,
931-
char: parser.source.codeUnitAt(matchStart),
932-
canOpen: true,
933-
canClose: false,
934-
syntax: this,
935-
endPos: matchEnd));
945+
node: text,
946+
length: runLength,
947+
char: parser.source.codeUnitAt(matchStart),
948+
canOpen: true,
949+
canClose: false,
950+
syntax: this,
951+
endPos: matchEnd,
952+
));
936953
parser.addNode(text);
937954
return true;
938955
}
939956

940-
final delimiterRun = DelimiterRun.tryParse(parser, matchStart, matchEnd,
941-
syntax: this,
942-
node: text,
943-
allowIntraWord: allowIntraWord,
944-
tags: tags ?? []);
957+
final delimiterRun = DelimiterRun.tryParse(
958+
parser,
959+
matchStart,
960+
matchEnd,
961+
syntax: this,
962+
node: text,
963+
allowIntraWord: allowIntraWord,
964+
tags: tags ?? [],
965+
);
945966
if (delimiterRun != null) {
946967
parser._pushDelimiter(delimiterRun);
947968
parser.addNode(text);
@@ -979,19 +1000,25 @@ class EmphasisSyntax extends DelimiterSyntax {
9791000

9801001
/// Parses `**strong**` and `*emphasis*`.
9811002
EmphasisSyntax.asterisk()
982-
: super(r'\*+',
983-
requiresDelimiterRun: true, allowIntraWord: true, tags: _tags);
1003+
: super(
1004+
r'\*+',
1005+
requiresDelimiterRun: true,
1006+
allowIntraWord: true,
1007+
tags: _tags,
1008+
);
9841009

9851010
static final _tags = [DelimiterTag('em', 1), DelimiterTag('strong', 2)];
9861011
}
9871012

9881013
/// Matches strikethrough syntax according to the GFM spec.
9891014
class StrikethroughSyntax extends DelimiterSyntax {
9901015
StrikethroughSyntax()
991-
: super('~+',
992-
requiresDelimiterRun: true,
993-
allowIntraWord: true,
994-
tags: [DelimiterTag('del', 2)]);
1016+
: super(
1017+
'~+',
1018+
requiresDelimiterRun: true,
1019+
allowIntraWord: true,
1020+
tags: [DelimiterTag('del', 2)],
1021+
);
9951022
}
9961023

9971024
@Deprecated('Use DelimiterSyntax instead')
@@ -1006,11 +1033,11 @@ class LinkSyntax extends DelimiterSyntax {
10061033

10071034
final Resolver linkResolver;
10081035

1009-
LinkSyntax(
1010-
{Resolver? linkResolver,
1011-
String pattern = r'\[',
1012-
int startCharacter = $lbracket})
1013-
: linkResolver = (linkResolver ?? ((String _, [String? __]) => null)),
1036+
LinkSyntax({
1037+
Resolver? linkResolver,
1038+
String pattern = r'\[',
1039+
int startCharacter = $lbracket,
1040+
}) : linkResolver = (linkResolver ?? ((String _, [String? __]) => null)),
10141041
super(pattern, startCharacter: startCharacter);
10151042

10161043
@override
@@ -1041,8 +1068,11 @@ class LinkSyntax extends DelimiterSyntax {
10411068
final leftParenIndex = parser.pos;
10421069
final inlineLink = _parseInlineLink(parser);
10431070
if (inlineLink != null) {
1044-
return _tryCreateInlineLink(parser, inlineLink,
1045-
getChildren: getChildren);
1071+
return _tryCreateInlineLink(
1072+
parser,
1073+
inlineLink,
1074+
getChildren: getChildren,
1075+
);
10461076
}
10471077
// At this point, we've matched `[...](`, but that `(` did not pan out to
10481078
// be an inline link. We must now check if `[...]` is simply a shortcut
@@ -1088,12 +1118,17 @@ class LinkSyntax extends DelimiterSyntax {
10881118
///
10891119
/// [label] does not need to be normalized.
10901120
Node? _resolveReferenceLink(
1091-
String label, Map<String, LinkReference> linkReferences,
1092-
{required List<Node> Function() getChildren}) {
1121+
String label,
1122+
Map<String, LinkReference> linkReferences, {
1123+
required List<Node> Function() getChildren,
1124+
}) {
10931125
final linkReference = linkReferences[normalizeLinkLabel(label)];
10941126
if (linkReference != null) {
1095-
return _createNode(linkReference.destination, linkReference.title,
1096-
getChildren: getChildren);
1127+
return _createNode(
1128+
linkReference.destination,
1129+
linkReference.title,
1130+
getChildren: getChildren,
1131+
);
10971132
} else {
10981133
// This link has no reference definition. But we allow users of the
10991134
// library to specify a custom resolver function ([linkResolver]) that
@@ -1115,8 +1150,11 @@ class LinkSyntax extends DelimiterSyntax {
11151150
}
11161151

11171152
/// Create the node represented by a Markdown link.
1118-
Node _createNode(String destination, String? title,
1119-
{required List<Node> Function() getChildren}) {
1153+
Node _createNode(
1154+
String destination,
1155+
String? title, {
1156+
required List<Node> Function() getChildren,
1157+
}) {
11201158
final children = getChildren();
11211159
final element = Element('a', children);
11221160
element.attributes['href'] = escapeAttribute(destination);
@@ -1129,17 +1167,26 @@ class LinkSyntax extends DelimiterSyntax {
11291167
/// Tries to create a reference link node.
11301168
///
11311169
/// Returns the link if it was successfully created, `null` otherwise.
1132-
Node? _tryCreateReferenceLink(InlineParser parser, String label,
1133-
{required List<Node> Function() getChildren}) {
1134-
return _resolveReferenceLink(label, parser.document.linkReferences,
1135-
getChildren: getChildren);
1170+
Node? _tryCreateReferenceLink(
1171+
InlineParser parser,
1172+
String label, {
1173+
required List<Node> Function() getChildren,
1174+
}) {
1175+
return _resolveReferenceLink(
1176+
label,
1177+
parser.document.linkReferences,
1178+
getChildren: getChildren,
1179+
);
11361180
}
11371181

11381182
// Tries to create an inline link node.
11391183
//
11401184
/// Returns the link if it was successfully created, `null` otherwise.
1141-
Node _tryCreateInlineLink(InlineParser parser, InlineLink link,
1142-
{required List<Node> Function() getChildren}) {
1185+
Node _tryCreateInlineLink(
1186+
InlineParser parser,
1187+
InlineLink link, {
1188+
required List<Node> Function() getChildren,
1189+
}) {
11431190
return _createNode(link.destination, link.title, getChildren: getChildren);
11441191
}
11451192

@@ -1413,13 +1460,17 @@ class LinkSyntax extends DelimiterSyntax {
14131460
class ImageSyntax extends LinkSyntax {
14141461
ImageSyntax({Resolver? linkResolver})
14151462
: super(
1416-
linkResolver: linkResolver,
1417-
pattern: r'!\[',
1418-
startCharacter: $exclamation);
1463+
linkResolver: linkResolver,
1464+
pattern: r'!\[',
1465+
startCharacter: $exclamation,
1466+
);
14191467

14201468
@override
1421-
Element _createNode(String destination, String? title,
1422-
{required List<Node> Function() getChildren}) {
1469+
Element _createNode(
1470+
String destination,
1471+
String? title, {
1472+
required List<Node> Function() getChildren,
1473+
}) {
14231474
final element = Element.empty('img');
14241475
final children = getChildren();
14251476
element.attributes['src'] = destination;

0 commit comments

Comments
 (0)