Skip to content

Commit 3e9cae3

Browse files
authored
fix(plugin-code-blocks): fix region interpolation incorrectly parsed (#304)
* fix(plugin-code-blocks): fix region interpolation incorrectly parsed Closes #291 * docs(plugin-code-blocks): update readme
1 parent 582c2b9 commit 3e9cae3

File tree

7 files changed

+32
-13
lines changed

7 files changed

+32
-13
lines changed

packages/plugin-code-blocks/README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ For more infos, please refer to [the documentation](https://knodescommunity.gith
2525

2626
## Usage
2727

28-
In any markdown content, (in README, pages, or doc comments), use the `{@codeblock ...}` & `{@inlineCodeblock ...}` macros to use code blocks.
28+
In the *source code* you want to use as *code blocks*, delimitate *regions*. *Regions* are started with `// #region my-name`, & ended with `// #endregion [my-name]`. Interleaved/nested *regions* are supported. Regions **must** only contain `[a-zA-Z0-9-_.]`.
29+
30+
In any *markdown* content, (in README, pages, or doc comments), use the `{@codeblock ...}` & `{@inlineCodeblock ...}` tags to use *code blocks*.
2931

3032
### Reference a file
3133

@@ -35,11 +37,12 @@ Syntax:
3537
{@codeblock <path-to-file>[#region] [mode] [ | custom-file-name]}
3638
```
3739

38-
- `<path-to-file>`: A path to the code file to embed. Checkout [this documentation page](https://knodescommunity.github.io/typedoc-plugins/_knodes_typedoc_pluginutils/pages/resolving-paths.html) for more infos on the syntax of the path.
39-
- `[#<region>]`: A named region in the target file. Regions are started with `// #region my-name`, & ended with `// #endregion [my-name]`. Interleaved/nested regions are supported. Note that region markers are not outputted in the generated code block. The `<region>` parameter can be a glob pattern, or a list of block names/patterns separated by a `+`.
40+
- `<path-to-file>`: A path to the code file to embed. Checkout [this documentation page](https://knodescommunity.github.io/typedoc-plugins/_knodes_typedoc_pluginutils/pages/resolving-paths.html) for more infos on the syntax of the path for monorepo configuration.
41+
> Note that the path **must not** contain any of the following characters: `#|`
42+
- `[#<region>]`: A region selector in the target file. The `<region>` parameter can be a glob pattern, or a list of block names/patterns separated by a `+`. See [minimatch](https://www.npmjs.com/package/minimatch) for details on supported patterns.
43+
> Note that region markers are not outputted in the generated code block.
4044
- `[mode]`: optional. Can be any valid {@link EBlockMode}, to override the default settings.
4145
- `[ | custom-file-name]`: allow to specify an explicit file name to display in the code block header.
42-
-
4346

4447
`{@codeblock ...}` are by default looked up into your [*workspace*](https://knodescommunity.github.io/typedoc-plugins/_knodes_typedoc_pluginutils/pages/resolving-paths.html) `examples` folder, but you can customize it by using the [`source` option](https://knodescommunity.github.io/typedoc-plugins/_knodes_typedoc_plugin_code_blocks/pages/options.html)
4548

packages/plugin-code-blocks/__tests__/integration/__snapshots__/simple.spec.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,9 +1043,9 @@ exports[`Codeblock Inline tag \`classes/BlocInline.TestInlineExplicitProject.htm
10431043
<p>A test code block for project path implicitly in <code>examples</code> directory</p>
10441044
<details class="code-block" open>
10451045
<summary>
1046-
<p>From ./examples/example-test.json</p>
1046+
<p>From ./examples/complex-regions.js#1~8</p>
10471047
</summary>
1048-
<pre><code class="language-json"><span class="hl-0">{</span><span class="hl-1">&quot;Hello&quot;</span><span class="hl-0">: </span><span class="hl-2">&quot;World&quot;</span><span class="hl-0">}</span>
1048+
<pre><code class="language-js"><span class="hl-3">const</span><span class="hl-0"> </span><span class="hl-4">foo</span><span class="hl-0"> = </span><span class="hl-2">&#39;FOO&#39;</span><br/><span class="hl-5">// ...</span><br/><span class="hl-3">const</span><span class="hl-0"> </span><span class="hl-4">bar</span><span class="hl-0"> = </span><span class="hl-2">&#39;BAR&#39;</span>
10491049
</code></pre>
10501050
</details>
10511051
</div>

packages/plugin-code-blocks/__tests__/integration/simple.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ describe( 'Codeblock', () => {
7676

7777
const codeblocks = dom.window.document.querySelectorAll( '.code-block' );
7878
expect( codeblocks ).toHaveLength( 1 );
79-
expect( codeblocks[0] ).toSatisfy( matchExpanded( './examples/example-test.json', SAMPLES['example-test.json'] ) );
79+
expect( codeblocks[0] ).toSatisfy( matchExpanded( './examples/complex-regions.js#1~8', "const foo = 'FOO'// ...const bar = 'BAR'" ) );
8080
} ) );
8181
it( 'should have constant content', withContent( content => {
8282
expect( formatHtml( content ) ).toMatchSnapshot();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// #region foo
2+
const foo = 'FOO'
3+
// #endregion foo
4+
5+
// #region ba-stuff
6+
// #region bar
7+
const bar = 'BAR'
8+
// #endregion bar
9+
const baz = 'BAZ'
10+
// #endregion ba-stuff

packages/plugin-code-blocks/__tests__/mock-fs/simple/src/codeblock/inline.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class TestInlineRelativeModule{}
1818
/**
1919
* A test code block for project path implicitly in `examples` directory
2020
*
21-
* {@codeblock ~~:example-test.json}
21+
* {@codeblock ~~:complex-regions.js#{foo,bar}}
2222
*/
2323
export class TestInlineExplicitProject{}
2424
// #endregion

packages/plugin-code-blocks/src/output/markdown-code-blocks.spec.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,23 @@ describe( 'Behavior', () => {
6767
withGitHub: boolean;
6868
}
6969
const defaultBlock = { startLine: 1, endLine: 1, file: FILE, region: DEFAULT_BLOCK_NAME as string };
70-
const helloRegion: IBlockGenerationAssertion['blocks'] = [[ 'hello', { code: 'Content of foo/qux.txt', startLine: 13, endLine: 24 } ]];
70+
const regions: IBlockGenerationAssertion['blocks'] = [
71+
[ 'hello', { code: 'Content of foo/qux.txt', startLine: 13, endLine: 24 } ],
72+
[ 'world', { code: 'stuff', startLine: 18, endLine: 42 } ],
73+
];
7174
it.each<[label: string, source: string, assertion: Partial<IBlockGenerationAssertion>]>( [
7275
[ 'Mode ⇒ code block', `{@codeblock ${FILE} default}`, { renderCall: { mode: EBlockMode.DEFAULT }} ],
7376
[ 'Mode ⇒ foldable code block', `{@codeblock ${FILE} expanded}`, { renderCall: { mode: EBlockMode.EXPANDED }} ],
7477
[ 'Mode ⇒ folded code block', `{@codeblock ${FILE} folded}`, { renderCall: { mode: EBlockMode.FOLDED }} ],
7578
[ 'Filename ⇒ default', `{@codeblock ${FILE}}`, { renderCall: { asFile: `./${FILE}` }} ],
7679
[ 'Filename ⇒ explicit', `{@codeblock ${FILE} | hello.txt}`, { renderCall: { asFile: 'hello.txt' }} ],
77-
[ 'Filename ⇒ region', `{@codeblock ${FILE}#hello}`, { renderCall: { asFile: `./${FILE}#13~24` }, blocks: helloRegion } ],
78-
[ 'Filename ⇒ region + explicit', `{@codeblock ${FILE}#hello | test.txt}`, { renderCall: { asFile: 'test.txt' }, blocks: helloRegion } ],
80+
[ 'Filename ⇒ region', `{@codeblock ${FILE}#hello}`, { renderCall: { asFile: `./${FILE}#13~24` }, blocks: [ regions[0] ] } ],
81+
[ 'Filename ⇒ region ({a,b})', `{@codeblock ${FILE}#{hello,world}}`, { renderCall: { asFile: `./${FILE}#13~42` }, blocks: regions } ],
82+
[ 'Filename ⇒ region (a+b)', `{@codeblock ${FILE}#hello+world}`, { renderCall: { asFile: `./${FILE}#13~42` }, blocks: regions } ],
83+
[ 'Filename ⇒ region + explicit', `{@codeblock ${FILE}#hello | test.txt}`, { renderCall: { asFile: 'test.txt' }, blocks: [ regions[0] ] } ],
7984
[ 'URL ⇒ default', `{@codeblock ${FILE}}`, { withGitHub: true, renderCall: { url: `${FakeSource.REPO_URL}/${FILE}#L1` }} ],
80-
[ 'URL ⇒ region', `{@codeblock ${FILE}#hello}`, { withGitHub: true, renderCall: { url: `${FakeSource.REPO_URL}/${FILE}#L13` }, blocks: helloRegion } ],
85+
[ 'URL ⇒ region', `{@codeblock ${FILE}#hello}`, { withGitHub: true, renderCall: { url: `${FakeSource.REPO_URL}/${FILE}#L13` }, blocks: [ regions[0] ] } ],
86+
[ 'Filename ⇒ region+mode+name', `{@codeblock ${FILE}#*o* folded | qux}`, { renderCall: { asFile: 'qux', mode: EBlockMode.FOLDED }, blocks: regions } ],
8187
] )( 'Code block "%s"', ( _label, source, { renderCall, blocks, withGitHub } ) => {
8288
if( withGitHub ){
8389
plugin.application.converter.addComponent( 'source', FakeSource as any );

packages/plugin-code-blocks/src/output/markdown-code-blocks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const comments = Object.entries( {
2525
} );
2626
const getFirstLineIndent = ( lines?: string[] ) => ( lines?.filter( l => l.trim() )[0]?.match( /^\s*/ ) ?? [ '' ] )[0];
2727
const CODEBLOCK_KIND = reflectionKindUtils.addReflectionKind( name, 'CodeBlock' ) as ReflectionKind;
28-
const EXTRACT_CODE_BLOCKS_REGEX = /(\S+?\w+?)(?:#(.+?))?(?:\s+(\w+))?(?:\s*\|\s*(.*?))?\s*/;
28+
const EXTRACT_CODE_BLOCKS_REGEX = /(?<file>\S+?\w+?)(?:#(?<regions>[^|\n\s]+))?(?:\s+(?<mode>\w+))?(?:\s*\|\s*(?<name>.*?))?/;
2929
const EXTRACT_INLINE_CODE_BLOCKS_REGEX = /(\S+?)(?:\s+(\w+))?\s*(```(\w+)?.*?```)/s;
3030
export class MarkdownCodeBlocks implements IPluginComponent<CodeBlockPlugin>{
3131
private readonly _logger = this.plugin.logger.makeChildLogger( MarkdownCodeBlocks.name );

0 commit comments

Comments
 (0)