Skip to content

Commit

Permalink
Merge pull request #20 from zakhenry/feat/support-json
Browse files Browse the repository at this point in the history
feat(Language Support): Add support for json and json5
  • Loading branch information
zakhenry authored Jul 7, 2019
2 parents 30c34f0 + 7613800 commit 013de58
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Here's a list of file types supported by this utility, if you have a need for an
contribute, it is easy!

```ts
// src/embedme.lib.ts#L44-L68
// src/embedme.lib.ts#L44-L70

enum SupportedFileType {
PLAIN_TEXT = 'txt',
Expand All @@ -93,6 +93,8 @@ enum SupportedFileType {
XML = 'xml',
MARKDOWN = 'md',
YAML = 'yaml',
JSON = 'json',
JSON_5 = 'json5',
PYTHON = 'py',
BASH = 'bash',
SHELL = 'sh',
Expand Down
6 changes: 6 additions & 0 deletions src/embedme.lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ enum SupportedFileType {
XML = 'xml',
MARKDOWN = 'md',
YAML = 'yaml',
JSON = 'json',
JSON_5 = 'json5',
PYTHON = 'py',
BASH = 'bash',
SHELL = 'sh',
Expand All @@ -68,12 +70,14 @@ enum SupportedFileType {
}

enum CommentFamily {
NONE, // some languages do not support comments, e.g. JSON
C,
XML,
HASH,
}

const languageMap: Record<CommentFamily, SupportedFileType[]> = {
[CommentFamily.NONE]: [SupportedFileType.JSON],
[CommentFamily.C]: [
SupportedFileType.PLAIN_TEXT, // this is a lie, but we gotta pick something
SupportedFileType.C,
Expand All @@ -90,6 +94,7 @@ const languageMap: Record<CommentFamily, SupportedFileType[]> = {
SupportedFileType.SWIFT,
SupportedFileType.KOTLIN,
SupportedFileType.SCALA,
SupportedFileType.JSON_5,
],
[CommentFamily.XML]: [SupportedFileType.HTML, SupportedFileType.MARKDOWN, SupportedFileType.XML],
[CommentFamily.HASH]: [
Expand All @@ -102,6 +107,7 @@ const languageMap: Record<CommentFamily, SupportedFileType[]> = {
};

const filetypeCommentReaders: Record<CommentFamily, FilenameFromCommentReader> = {
[CommentFamily.NONE]: _ => null,
[CommentFamily.C]: line => {
const match = line.match(/\/\/\s?(\S*?$)/m);
if (!match) {
Expand Down
26 changes: 26 additions & 0 deletions test/fixture.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,26 @@ hello:
- world
```
JSON
<!-- embedme sample.json -->
```json
{
"hello": "world"
}
```

JSON5

```json5
// sample.json

{
hello: 'world',
}
```

Ruby

```rb
Expand Down Expand Up @@ -371,3 +391,9 @@ Ignored block
```ts
// sample.ts#L1-2
```

### missing comment on language embed with no comment support

```json

```
3 changes: 3 additions & 0 deletions test/sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello": "world"
}

0 comments on commit 013de58

Please sign in to comment.