From 186836e4b7d63b12511504fcc3218756fa3e41ed Mon Sep 17 00:00:00 2001 From: Zak Henry Date: Sat, 13 Jul 2019 22:11:05 +0100 Subject: [PATCH 1/4] feat(Lib): Restructure so comment overrides always take precedence --- src/embedme.lib.ts | 75 ++++++++++++++++++++++++---------------------- test/fixture.md | 8 +++++ 2 files changed, 48 insertions(+), 35 deletions(-) diff --git a/src/embedme.lib.ts b/src/embedme.lib.ts index abc2f12..bddd203 100644 --- a/src/embedme.lib.ts +++ b/src/embedme.lib.ts @@ -189,50 +189,55 @@ function getReplacement( logMethod(logPrefix, ...messages); }; - if (!codeExtension) { - log({ returnSnippet: substr }, chalk.blue(`No code extension detected, skipping code block...`)); - return substr; - } - - if (!firstLine && !commentEmbedOverrideFilepath) { - log({ returnSnippet: substr }, chalk.blue(`Code block is empty & no preceding embedme comment, skipping...`)); - return substr; - } - if (ignoreNext) { log({ returnSnippet: substr }, chalk.blue(`"Ignore next" comment detected, skipping code block...`)); return substr; } - const supportedFileTypes: SupportedFileType[] = Object.values(SupportedFileType).filter(x => typeof x === 'string'); - - if (supportedFileTypes.indexOf(codeExtension) < 0) { - log( - { returnSnippet: substr }, - chalk.yellow( - `Unsupported file extension [${codeExtension}], supported extensions are ${supportedFileTypes.join( - ', ', - )}, skipping code block`, - ), - ); + if (!codeExtension) { + log({ returnSnippet: substr }, chalk.blue(`No code extension detected, skipping code block...`)); return substr; } - const languageFamily: CommentFamily | null = lookupLanguageCommentFamily(codeExtension); + let commentedFilename: string | null; + if (commentEmbedOverrideFilepath) { + commentedFilename = commentEmbedOverrideFilepath; + } else { + if (!firstLine) { + log({ returnSnippet: substr }, chalk.blue(`Code block is empty & no preceding embedme comment, skipping...`)); + return substr; + } - if (languageFamily == null) { - log( - { returnSnippet: substr }, - chalk.red( - `File extension ${chalk.underline( - codeExtension, - )} marked as supported, but comment family could not be determined. Please report this issue.`, - ), - ); - return substr; - } + const supportedFileTypes: SupportedFileType[] = Object.values(SupportedFileType).filter(x => typeof x === 'string'); + + if (supportedFileTypes.indexOf(codeExtension) < 0) { + log( + { returnSnippet: substr }, + chalk.yellow( + `Unsupported file extension [${codeExtension}], supported extensions are ${supportedFileTypes.join( + ', ', + )}, skipping code block`, + ), + ); + return substr; + } - const commentedFilename = commentEmbedOverrideFilepath || filetypeCommentReaders[languageFamily](firstLine); + const languageFamily: CommentFamily | null = lookupLanguageCommentFamily(codeExtension); + + if (languageFamily == null) { + log( + { returnSnippet: substr }, + chalk.red( + `File extension ${chalk.underline( + codeExtension, + )} marked as supported, but comment family could not be determined. Please report this issue.`, + ), + ); + return substr; + } + + commentedFilename = filetypeCommentReaders[languageFamily](firstLine); + } if (!commentedFilename) { log( @@ -245,7 +250,7 @@ function getReplacement( const matches = commentedFilename.match(/\s?(\S+?)((#L(\d+)-L(\d+))|$)/m); if (!matches) { - log({ returnSnippet: substr }, chalk.gray(`No file found in first comment block`)); + log({ returnSnippet: substr }, chalk.gray(`No file found in embed line`)); return substr; } diff --git a/test/fixture.md b/test/fixture.md index a08a472..9b645e5 100644 --- a/test/fixture.md +++ b/test/fixture.md @@ -411,6 +411,14 @@ export function hello(): string { } ``` +### Embed with comment and unknown file type + + + +```{.json caption="Some JSON file"} +{"key": true} +``` + ## Errors ### Empty block From fa45cbd811e18f82d99bd2fc12042f5f7cc0549e Mon Sep 17 00:00:00 2001 From: Zak Henry Date: Sat, 13 Jul 2019 22:20:16 +0100 Subject: [PATCH 2/4] test(*): refactor tests to verify output, not just no-changes --- package.json | 2 +- test/fixture-source.md | 292 +++++++++++++++++++++++++++++++++++++++++ test/fixture.md | 11 +- 3 files changed, 300 insertions(+), 5 deletions(-) create mode 100644 test/fixture-source.md diff --git a/package.json b/package.json index c062d97..89865f4 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "build": "tsc", "write-help-output": "echo \"$ embedme --help\" > readme/help-output.txt && node dist/embedme.js --help >> readme/help-output.txt", "start": "node dist/embedme.js README.md", - "test:write": "node dist/embedme.js test/fixture.md && yarn prettier:write", + "test:write": "node dist/embedme.js test/fixture-source.md --stdout > test/fixture.md && node dist/embedme.js test/fixture.md --check && yarn prettier:write", "test:check": "! git status | grep test/fixture.md || (echo 'You must commit build and commit changes to test/fixture.md!' && exit 1)", "test": "yarn test:write && yarn test:check", "commit": "git add . && git-cz", diff --git a/test/fixture-source.md b/test/fixture-source.md new file mode 100644 index 0000000..841358c --- /dev/null +++ b/test/fixture-source.md @@ -0,0 +1,292 @@ +# File types + +Plain Text + +```txt +// sample.txt +``` + +Typescript + +```ts +// sample.ts +``` + +Javascript + +```js +// sample.js +``` + +HTML + +```html + +``` + +Python + +```py +# sample.py +``` + +Rust + +```rust +// sample.rs + +``` + +C++ + +```cpp +// sample.cpp +``` + +C + +```c +// sample.c +``` + +Java + +```java +// sample.java +``` + +Golang + +```go +// sample.go +``` + +Bash + +```sh +# sample.sh +``` + +Shell + +```sh +# sample.sh +``` + +Objective C + +```objectivec +// sample.m +``` + +SCSS + +```scss +// sample.scss +``` + +PHP + +```php +// sample.php +``` + +C# + +```cs +// sample.cs +``` + +Swift + +```swift +// sample.swift +``` + +XML + +```xml + +``` + +Yaml + +```yaml +# sample.yaml +``` + +JSON + + + +```json + +``` + +JSON5 + +```json5 +// sample.json5 +``` + +Ruby + +```rb +# sample.rb +``` + +Crystal + +```cr +# sample.cr +``` + +Kotlin + +```kotlin +// sample.kt +``` + +Scala + +```scala +// sample.scala +``` + +Plant UML + +```puml +' sample.puml +``` + +Mermaid + +```mermaid +%% sample.mermaid +``` + +Protobuf + +```proto +// sample.proto +``` + +CMake + +```cmake +# sample.cmake +``` + +## Extension-less selection + +```sh +# sample +``` + +## Line selection + +```cs +// sample.cs#L6-L13 +``` + +## Indented selection + + ```ts + // sample.ts + ``` + +## Embedme Ignore + + + +```ts +// sample.ts +``` + +## Embedme Ignore alt syntax + + + +```ts +// sample.ts +``` + +## Embed with comment + + + +```ts +``` + +### Embed with comment and unknown file type + + + +```{.json caption="Some JSON file"} + +``` + +## Errors + +### Empty block + +```ts +``` + +### No file handler + +```binary +01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01101100 01100100 +``` + +### No file extension + +``` +Ignored block +``` + +### Bad file format + +```ts +// Not a file +``` + +### Also bad file format + +```ts +// also-not-a-file +``` + +### Missing file + +```txt +// this-file-does-not-exist.txt +``` + +### Contains Codefence + +```md + +``` + +### Contains Codefence, but not the embedded lines + +```md + + +# This markdown document + +## Contains a codefence +``` + +### malformed line numbering + +```ts +// sample.ts#L1-2 +``` + +### missing comment on language embed with no comment support + +```json + +``` diff --git a/test/fixture.md b/test/fixture.md index 9b645e5..62fa7c0 100644 --- a/test/fixture.md +++ b/test/fixture.md @@ -257,7 +257,7 @@ puts 'Hello, world!' ``` -Ruby +Crystal ```cr # sample.cr @@ -413,10 +413,13 @@ export function hello(): string { ### Embed with comment and unknown file type - + + +```{.json +{ + "hello": "world" +} -```{.json caption="Some JSON file"} -{"key": true} ``` ## Errors From 6eed22236265e69ccf04eba7534ca84f9de9c55c Mon Sep 17 00:00:00 2001 From: Zak Henry Date: Sat, 13 Jul 2019 22:20:54 +0100 Subject: [PATCH 3/4] fix(PlantUML): Fix Plant UML embed comment lookup --- src/embedme.lib.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/embedme.lib.ts b/src/embedme.lib.ts index bddd203..59f0ec9 100644 --- a/src/embedme.lib.ts +++ b/src/embedme.lib.ts @@ -141,7 +141,7 @@ const filetypeCommentReaders: Record = return match[1]; }, [CommentFamily.HASH]: leadingSymbol('#'), - [CommentFamily.SINGLE_QUOTE]: leadingSymbol('//'), + [CommentFamily.SINGLE_QUOTE]: leadingSymbol(`'`), [CommentFamily.DOUBLE_PERCENT]: leadingSymbol('%%'), }; From 7a2ac3b6e3f2f957dce442b27418f91e0173d8e0 Mon Sep 17 00:00:00 2001 From: Zak Henry Date: Sat, 13 Jul 2019 22:28:27 +0100 Subject: [PATCH 4/4] fix(Lib): Relax regex on extension mapping to support more complex types Relates to #30 --- src/embedme.lib.ts | 14 +++++++------- test/fixture.md | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/embedme.lib.ts b/src/embedme.lib.ts index 59f0ec9..e50c365 100644 --- a/src/embedme.lib.ts +++ b/src/embedme.lib.ts @@ -194,15 +194,15 @@ function getReplacement( return substr; } - if (!codeExtension) { - log({ returnSnippet: substr }, chalk.blue(`No code extension detected, skipping code block...`)); - return substr; - } - let commentedFilename: string | null; if (commentEmbedOverrideFilepath) { commentedFilename = commentEmbedOverrideFilepath; } else { + if (!codeExtension) { + log({ returnSnippet: substr }, chalk.blue(`No code extension detected, skipping code block...`)); + return substr; + } + if (!firstLine) { log({ returnSnippet: substr }, chalk.blue(`Code block is empty & no preceding embedme comment, skipping...`)); return substr; @@ -346,7 +346,7 @@ function getReplacement( return substr; } - const chalkColour = options.verify ? 'red' : 'green'; + const chalkColour = options.verify ? 'yellow' : 'green'; log( { returnSnippet: replacement }, @@ -383,7 +383,7 @@ export function embedme(sourceText: string, inputFilePath: string, options: Embe const [codeFence, leadingSpaces] = result; const start = sourceText.substring(previousEnd, result.index); - const extensionMatch = codeFence.match(/```(\S*)/); + const extensionMatch = codeFence.match(/```(.*)/); const codeExtension = extensionMatch ? extensionMatch[1] : null; const splitFence = codeFence.split('\n'); diff --git a/test/fixture.md b/test/fixture.md index 62fa7c0..148c4a0 100644 --- a/test/fixture.md +++ b/test/fixture.md @@ -415,7 +415,7 @@ export function hello(): string { -```{.json +```{.json caption="Some JSON file"} { "hello": "world" }