Skip to content

Commit

Permalink
Account for nested markdown code blocks in code excerpt tool (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
parlough authored Oct 26, 2023
1 parent ee4732c commit 521a1b3
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 7 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
*.iml
.idea
.vscode
.DS_Store

.packages
.pub
.dart_tool
build/

build
21 changes: 16 additions & 5 deletions packages/code_excerpt_updater/lib/src/code_excerpt_updater.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ import 'logger.dart';
/// [fragmentDirPath] directory, and diff sources from `srcDirPath`.
class Updater {
static final RegExp codeBlockStartMarker =
RegExp(r'^\s*(///?)?\s*(```|{%-?\s*\w+\s*(\w*)(\s+.*)?-?%})?');
static final RegExp codeBlockEndMarker = RegExp(r'^\s*(///?)?\s*(```)?');
RegExp(r'^\s*(///?)?\s*(```+|{%-?\s*\w+\s*(\w*)(\s+.*)?-?%})?');
static final RegExp codeBlockEndPrettifyMarker =
RegExp(r'^\s*(///?)?\s*({%-?\s*end\w+\s*-?%})?');

Expand Down Expand Up @@ -217,9 +216,18 @@ class Updater {
return <String>[openingCodeBlockLine];
}

final codeBlockEndChecker = firstLineMatch[2]?.startsWith('`') == true
? codeBlockEndMarker
: codeBlockEndPrettifyMarker;
final RegExp codeBlockEndChecker;
final codeBlockStart = firstLineMatch[2];
if (codeBlockStart != null && codeBlockStart.startsWith('`')) {
// Create a RegExp to check for at least the same amount of backticks
// as the opening backticks had.
final backtickCount = codeBlockStart.length;
codeBlockEndChecker = _createCodeBlockEndMarker(backtickCount);
} else {
// Fall back to the prettify end marker for now.
codeBlockEndChecker = codeBlockEndPrettifyMarker;
}

String? closingCodeBlockLine;
while (_lines.isNotEmpty) {
line = _lines[0];
Expand Down Expand Up @@ -350,4 +358,7 @@ class Updater {
if (ext.startsWith('.')) ext = ext.substring(1);
return ext;
}

static RegExp _createCodeBlockEndMarker(int backtickCount) =>
RegExp(r'^\s*(///?)?\s*' '(`{$backtickCount})?');
}
2 changes: 2 additions & 0 deletions packages/code_excerpt_updater/test/main_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ void testsFromDefaultDir() {
'basic_no_region.dart',
'basic_with_args.md',
'basic_with_region.dart',
'dartdoc.md',
'diff.md',
'frag_not_found.dart',
'invalid_code_block.dart',
Expand All @@ -152,6 +153,7 @@ void testsFromDefaultDir() {
'basic_no_region.dart',
'basic_with_empty_region.md',
'basic_with_region.dart',
'dartdoc.md',
'escape_ng_interpolation.md',
'fragment-indentation.md',
'language-tour.md',
Expand Down
12 changes: 12 additions & 0 deletions packages/code_excerpt_updater/test_data/expected/dartdoc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## API docs

Markdown code block opened and closed with more than 3 backticks
that contains another Markdown code block declared with just 3.

<?code-excerpt "backticks_in_api_doc.dart"?>
````dart
/// ```html
/// <h1>HTML is magical!</h1>
/// ```
class HTML {}
````
8 changes: 8 additions & 0 deletions packages/code_excerpt_updater/test_data/src/dartdoc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## API docs

Markdown code block opened and closed with more than 3 backticks
that contains another Markdown code block declared with just 3.

<?code-excerpt "backticks_in_api_doc.dart"?>
````dart
````
12 changes: 12 additions & 0 deletions packages/code_excerpt_updater/test_data/src/no_change/dartdoc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## API docs

Markdown code block opened and closed with more than 3 backticks
that contains another Markdown code block declared with just 3.

<?code-excerpt "backticks_in_api_doc.dart"?>
````dart
/// ```html
/// <h1>HTML is magical!</h1>
/// ```
class HTML {}
````

0 comments on commit 521a1b3

Please sign in to comment.