From 521a1b3962ae3688f2c4ac49e4ae42391ccdf458 Mon Sep 17 00:00:00 2001 From: Parker Lougheed Date: Thu, 26 Oct 2023 09:10:37 -0500 Subject: [PATCH] Account for nested markdown code blocks in code excerpt tool (#195) --- .gitignore | 5 +++-- .../lib/src/code_excerpt_updater.dart | 21 ++++++++++++++----- .../code_excerpt_updater/test/main_test.dart | 2 ++ .../test_data/expected/dartdoc.md | 12 +++++++++++ .../test_data/src/dartdoc.md | 8 +++++++ .../test_data/src/no_change/dartdoc.md | 12 +++++++++++ 6 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 packages/code_excerpt_updater/test_data/expected/dartdoc.md create mode 100644 packages/code_excerpt_updater/test_data/src/dartdoc.md create mode 100644 packages/code_excerpt_updater/test_data/src/no_change/dartdoc.md diff --git a/.gitignore b/.gitignore index 4d0bcc7..6b96cac 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,9 @@ *.iml .idea .vscode +.DS_Store -.packages -.pub +.dart_tool +build/ build diff --git a/packages/code_excerpt_updater/lib/src/code_excerpt_updater.dart b/packages/code_excerpt_updater/lib/src/code_excerpt_updater.dart index 3860dd1..68ab77e 100644 --- a/packages/code_excerpt_updater/lib/src/code_excerpt_updater.dart +++ b/packages/code_excerpt_updater/lib/src/code_excerpt_updater.dart @@ -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*-?%})?'); @@ -217,9 +216,18 @@ class Updater { return [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]; @@ -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})?'); } diff --git a/packages/code_excerpt_updater/test/main_test.dart b/packages/code_excerpt_updater/test/main_test.dart index 0212e51..6f25f52 100644 --- a/packages/code_excerpt_updater/test/main_test.dart +++ b/packages/code_excerpt_updater/test/main_test.dart @@ -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', @@ -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', diff --git a/packages/code_excerpt_updater/test_data/expected/dartdoc.md b/packages/code_excerpt_updater/test_data/expected/dartdoc.md new file mode 100644 index 0000000..5c08223 --- /dev/null +++ b/packages/code_excerpt_updater/test_data/expected/dartdoc.md @@ -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. + + +````dart +/// ```html +///

HTML is magical!

+/// ``` +class HTML {} +```` diff --git a/packages/code_excerpt_updater/test_data/src/dartdoc.md b/packages/code_excerpt_updater/test_data/src/dartdoc.md new file mode 100644 index 0000000..6b85d49 --- /dev/null +++ b/packages/code_excerpt_updater/test_data/src/dartdoc.md @@ -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. + + +````dart +```` diff --git a/packages/code_excerpt_updater/test_data/src/no_change/dartdoc.md b/packages/code_excerpt_updater/test_data/src/no_change/dartdoc.md new file mode 100644 index 0000000..5c08223 --- /dev/null +++ b/packages/code_excerpt_updater/test_data/src/no_change/dartdoc.md @@ -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. + + +````dart +/// ```html +///

HTML is magical!

+/// ``` +class HTML {} +````