Skip to content

Commit

Permalink
feat: add comments support
Browse files Browse the repository at this point in the history
Fix #133
  • Loading branch information
erikn69 committed Sep 11, 2023
1 parent 590c949 commit 16e7e5b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ The following options may be passed to the `highlight` function.
string: '\x1b[32m', // Strings
special: '\x1b[33m', // Special characters
bracket: '\x1b[33m', // Brackets (parentheses)
bracket: '\x1b[90m', // Comments
clear: '\x1b[0m' // Clear (inserted after each match)
}
```
Expand Down
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const DEFAULT_OPTIONS = {
string: '\x1b[32m',
special: '\x1b[33m',
bracket: '\x1b[33m',
comment: '\x1b[90m',
clear: '\x1b[0m'
}
}
Expand All @@ -28,6 +29,10 @@ const highlighters = [
group: 1,
regex: new RegExp(`(^|${SPLIT_CHARS})(${keywords.join('|')})(?=${SPLIT_CHARS}|$)`, 'gi')
},
{
name: 'comment',
regex: /(\/\*(?:\*|.)*?\*\/|--.*?(?:\n|$)|#.*?(?:\n|$))/gms
},
{
name: 'special',
regex: /(=|!=|%|\/|\*|-|,|;|:|\+|<|>)/g
Expand Down
2 changes: 2 additions & 0 deletions test/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ console.log(highlight('DROP PROCEDURE IF EXISTS `some-database`.`some-table`;'))
console.log(highlight('SELECT * FROM a;SELECT * FROM b;'))

console.log(highlight("select * from a where b = 'array<map<string,string>>';", { html: true }))

console.log(highlight('SELECT */* table.* */ FROM a;\nSELECT * FROM b; # comment\nSELECT * FROM c; -- comment\nSELECT * FROM d; /* multi\nline\ncomment */'))
31 changes: 31 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const OPTIONS = {
string: '[string]',
special: '[special]',
bracket: '[bracket]',
comment: '[comment]',
clear: '[clear]'
}
}
Expand Down Expand Up @@ -85,6 +86,21 @@ describe('unicode', () => {
.toBe('[bracket]([clear]f1[bracket])[clear]')
})

it('comment /**/', () => {
expect(hlUni('/*/*comment\nsecondline*/*/'))
.toBe('[comment]/*/*comment\nsecondline*/[clear][special]*[clear][special]/[clear]')
})

it('comment #', () => {
expect(hlUni('#comment\n#comment'))
.toBe('[comment]#comment\n[clear][comment]#comment[clear]')
})

it('comment --', () => {
expect(hlUni('--comment\n--comment'))
.toBe('[comment]--comment\n[clear][comment]--comment[clear]')
})

it('functions', () => {
expect(hlUni('COUNT(`id`)'))
.toBe('[function]COUNT[clear][bracket]([clear][string]`id`[clear][bracket])[clear]')
Expand Down Expand Up @@ -177,6 +193,21 @@ describe('html', () => {
.toBe('<span class="sql-hl-bracket">(</span>f1<span class="sql-hl-bracket">)</span>')
})

it('comment /**/', () => {
expect(hlHtml('/*/*comment\nsecondline*/*/'))
.toBe('<span class="sql-hl-comment">/*/*comment\nsecondline*/</span><span class="sql-hl-special">*</span><span class="sql-hl-special">/</span>')
})

it('comment #', () => {
expect(hlHtml('#comment\n#comment'))
.toBe('<span class="sql-hl-comment">#comment\n</span><span class="sql-hl-comment">#comment</span>')
})

it('comment --', () => {
expect(hlHtml('--comment\n--comment'))
.toBe('<span class="sql-hl-comment">--comment\n</span><span class="sql-hl-comment">--comment</span>')
})

it('functions', () => {
expect(hlHtml('COUNT(`id`)'))
.toBe('<span class="sql-hl-function">COUNT</span><span class="sql-hl-bracket">(</span><span class="sql-hl-string">`id`</span><span class="sql-hl-bracket">)</span>')
Expand Down

0 comments on commit 16e7e5b

Please sign in to comment.