Skip to content

Commit 98998c0

Browse files
committed
Add syntax support for MDX, SQL, TOML, and YAML formats with tests
1 parent f8adeca commit 98998c0

File tree

11 files changed

+464
-2
lines changed

11 files changed

+464
-2
lines changed

packages/block-parser/_usage.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const { parseBlocks } = require('./src')
2+
const { deepLog } = require('./test/logs')
3+
4+
const htmlAndMarkdown = `
5+
<!-- docs inlineExample foo={{ rad: 'bar' }}-->99<!--/docs-->
6+
7+
<!-- docs fooBar isCool -->
8+
Stuff inside the block
9+
<!--/docs-->
10+
`
11+
// const one = parseBlocks(htmlAndMarkdown, {
12+
// open: 'docs',
13+
// close: '/docs'
14+
// })
15+
16+
// deepLog(one.blocks)
17+
18+
19+
const yaml = `
20+
# This is a comment
21+
22+
foo: bar
23+
baz: 123
24+
25+
## CodeGen transform ##
26+
stuff that will be transformed
27+
## /CodeGen ##
28+
`
29+
const two = parseBlocks(yaml, {
30+
open: 'CodeGen',
31+
close: '/CodeGen',
32+
syntax: 'yaml'
33+
})
34+
deepLog(two)

packages/block-parser/src/syntax.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ const toml = {
7474
singleLineTag: '#',
7575
singleLinePattern: '#+',
7676
singleLine: '#.*$',
77-
content: '[ \\t\\S]*?'
77+
content: '[ \\t\\S]*?',
7878
}
7979

80+
8081
const syntaxMap = {
8182
// <!-- x -->
8283
md: html,
@@ -95,7 +96,7 @@ const syntaxMap = {
9596
// -- x
9697
sql: sql,
9798
// # x
98-
toml: toml
99+
toml: toml,
99100
}
100101

101102
// Additional comment syntaxes for future
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const path = require('path')
2+
const fs = require('fs').promises
3+
const { test } = require('uvu')
4+
const assert = require('uvu/assert')
5+
const { parseBlocks } = require('../src/index')
6+
const { deepLog } = require('./logs')
7+
const { normalizeBlocks } = require('./utils')
8+
9+
test('MDX file parse', async () => {
10+
const contents = await fs.readFile(path.join(__dirname, './fixtures/simple.mdx'), 'utf-8')
11+
const blocks = parseBlocks(contents, {
12+
syntax: 'mdx',
13+
open: 'GENERATED',
14+
close: 'END-GENERATED',
15+
})
16+
/*
17+
deepLog(blocks.blocks)
18+
/** */
19+
assert.equal(normalizeBlocks(blocks.blocks), [
20+
{
21+
index: 1,
22+
type: 'a',
23+
options: {},
24+
openValue: '{/* GENERATED a */}\n',
25+
contentValue: '// This is a comment inside',
26+
closeValue: '\n{/* END-GENERATED */}',
27+
rawArgs: '',
28+
rawContent: '// This is a comment inside',
29+
blockValue: '{/* GENERATED a */}\n// This is a comment inside\n{/* END-GENERATED */}'
30+
},
31+
{
32+
index: 2,
33+
type: 'b',
34+
options: {},
35+
openValue: '{/* GENERATED b */}\n',
36+
contentValue: '// Another comment block',
37+
closeValue: '\n{/* END-GENERATED */}',
38+
rawArgs: '',
39+
rawContent: '// Another comment block',
40+
blockValue: '{/* GENERATED b */}\n// Another comment block\n{/* END-GENERATED */}'
41+
},
42+
{
43+
index: 3,
44+
type: 'c',
45+
options: {},
46+
openValue: '{/* GENERATED c */}\n',
47+
contentValue: '// Multiline\n// comment block',
48+
closeValue: '\n{/* END-GENERATED */}',
49+
rawArgs: '',
50+
rawContent: '// Multiline\n// comment block',
51+
blockValue: '{/* GENERATED c */}\n// Multiline\n// comment block\n{/* END-GENERATED */}'
52+
},
53+
{
54+
index: 4,
55+
type: 'MyCodeGen',
56+
options: { foo: 'bar' },
57+
openValue: "{/* GENERATED MyCodeGen foo='bar' */}\n",
58+
contentValue: '// Generated content\nconst value = \'test\'',
59+
closeValue: '\n{/* END-GENERATED */}',
60+
rawArgs: "foo='bar'",
61+
rawContent: '// Generated content\nconst value = \'test\'',
62+
blockValue: "{/* GENERATED MyCodeGen foo='bar' */}\n// Generated content\nconst value = 'test'\n{/* END-GENERATED */}"
63+
}
64+
])
65+
})
66+
67+
test.run()
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const path = require('path')
2+
const fs = require('fs').promises
3+
const { test } = require('uvu')
4+
const assert = require('uvu/assert')
5+
const { parseBlocks } = require('../src/index')
6+
const { deepLog } = require('./logs')
7+
const { normalizeBlocks } = require('./utils')
8+
9+
test('SQL file parse', async () => {
10+
const contents = await fs.readFile(path.join(__dirname, './fixtures/simple.sql'), 'utf-8')
11+
const blocks = parseBlocks(contents, {
12+
syntax: 'sql',
13+
open: 'GENERATED',
14+
close: 'END-GENERATED',
15+
})
16+
/*
17+
deepLog(blocks.blocks)
18+
/** */
19+
assert.equal(normalizeBlocks(blocks.blocks), [
20+
{
21+
index: 1,
22+
type: 'a',
23+
options: {},
24+
openValue: '/* GENERATED a */\n',
25+
contentValue: '-- This is a comment inside',
26+
closeValue: '\n/* END-GENERATED */',
27+
rawArgs: '',
28+
rawContent: '-- This is a comment inside',
29+
blockValue: '/* GENERATED a */\n-- This is a comment inside\n/* END-GENERATED */'
30+
},
31+
{
32+
index: 2,
33+
type: 'b',
34+
options: {},
35+
openValue: '/* GENERATED b */\n',
36+
contentValue: '-- Another comment block',
37+
closeValue: '\n/* END-GENERATED */',
38+
rawArgs: '',
39+
rawContent: '-- Another comment block',
40+
blockValue: '/* GENERATED b */\n-- Another comment block\n/* END-GENERATED */'
41+
},
42+
{
43+
index: 3,
44+
type: 'c',
45+
options: {},
46+
openValue: '/* GENERATED c */\n',
47+
contentValue: '-- Multiline\n-- comment block',
48+
closeValue: '\n/* END-GENERATED */',
49+
rawArgs: '',
50+
rawContent: '-- Multiline\n-- comment block',
51+
blockValue: '/* GENERATED c */\n-- Multiline\n-- comment block\n/* END-GENERATED */'
52+
},
53+
{
54+
index: 4,
55+
type: 'MyCodeGen',
56+
options: { foo: 'bar' },
57+
openValue: "/* GENERATED MyCodeGen foo='bar' */\n",
58+
contentValue: '-- Generated content\nSELECT * FROM users WHERE id = 1;',
59+
closeValue: '\n/* END-GENERATED */',
60+
rawArgs: "foo='bar'",
61+
rawContent: '-- Generated content\nSELECT * FROM users WHERE id = 1;',
62+
blockValue: "/* GENERATED MyCodeGen foo='bar' */\n-- Generated content\nSELECT * FROM users WHERE id = 1;\n/* END-GENERATED */"
63+
}
64+
])
65+
})
66+
67+
test.run()
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
const path = require('path')
2+
const fs = require('fs').promises
3+
const { test } = require('uvu')
4+
const assert = require('uvu/assert')
5+
const { parseBlocks } = require('../src/index')
6+
const { deepLog } = require('./logs')
7+
const { normalizeBlocks } = require('./utils')
8+
9+
test('TOML file parse', async () => {
10+
const contents = await fs.readFile(path.join(__dirname, './fixtures/simple.toml'), 'utf-8')
11+
const blocks = parseBlocks(contents, {
12+
syntax: 'toml',
13+
open: 'GENERATED',
14+
close: 'END-GENERATED',
15+
})
16+
17+
/*
18+
deepLog(blocks.blocks)
19+
/** */
20+
21+
assert.equal(normalizeBlocks(blocks.blocks), normalizeBlocks([
22+
{
23+
index: 1,
24+
type: 'a',
25+
options: {},
26+
context: { isMultiline: true },
27+
open: { value: '# GENERATED a #\n', start: 25, end: 41 },
28+
content: {
29+
value: '# This is a comment inside',
30+
start: 41,
31+
end: 67,
32+
indentation: 0
33+
},
34+
close: { value: '\n# END-GENERATED #', start: 67, end: 85 },
35+
block: {
36+
indentation: '',
37+
lines: [ 3, 5 ],
38+
start: 25,
39+
end: 85,
40+
rawArgs: '',
41+
rawContent: '# This is a comment inside',
42+
value: '# GENERATED a #\n# This is a comment inside\n# END-GENERATED #'
43+
}
44+
},
45+
{
46+
index: 2,
47+
type: 'b',
48+
options: {},
49+
context: { isMultiline: true },
50+
open: { value: '# GENERATED b #\n', start: 87, end: 103 },
51+
content: {
52+
value: '# Another comment block',
53+
start: 103,
54+
end: 126,
55+
indentation: 0
56+
},
57+
close: { value: '\n# END-GENERATED #', start: 126, end: 144 },
58+
block: {
59+
indentation: '',
60+
lines: [ 7, 9 ],
61+
start: 87,
62+
end: 144,
63+
rawArgs: '',
64+
rawContent: '# Another comment block',
65+
value: '# GENERATED b #\n# Another comment block\n# END-GENERATED #'
66+
}
67+
},
68+
{
69+
index: 3,
70+
type: 'c',
71+
options: {},
72+
context: { isMultiline: true },
73+
open: { value: '# GENERATED c #\n', start: 146, end: 162 },
74+
content: {
75+
value: '# Multiline\n# comment block',
76+
start: 162,
77+
end: 189,
78+
indentation: 0
79+
},
80+
close: { value: '\n# END-GENERATED #', start: 189, end: 207 },
81+
block: {
82+
indentation: '',
83+
lines: [ 11, 14 ],
84+
start: 146,
85+
end: 207,
86+
rawArgs: '',
87+
rawContent: '# Multiline\n# comment block',
88+
value: '# GENERATED c #\n# Multiline\n# comment block\n# END-GENERATED #'
89+
}
90+
},
91+
{
92+
index: 4,
93+
type: 'MyCodeGen',
94+
options: { foo: 'bar' },
95+
context: { isMultiline: true },
96+
open: {
97+
value: "# GENERATED MyCodeGen foo='bar' #\n",
98+
start: 210,
99+
end: 244
100+
},
101+
content: {
102+
value: '# Comment\nvalue = "test"',
103+
},
104+
close: { value: '\n# END-GENERATED #', start: 268, end: 286 },
105+
block: {
106+
indentation: '',
107+
rawArgs: "foo='bar'",
108+
rawContent: '# Comment\nvalue = "test"',
109+
value: "# GENERATED MyCodeGen foo='bar' #\n" +
110+
'# Comment\n' +
111+
'value = "test"\n' +
112+
'# END-GENERATED #'
113+
}
114+
}])
115+
)
116+
})
117+
118+
test.run()
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const path = require('path')
2+
const fs = require('fs').promises
3+
const { test } = require('uvu')
4+
const assert = require('uvu/assert')
5+
const { parseBlocks } = require('../src/index')
6+
const { deepLog } = require('./logs')
7+
const { normalizeBlocks } = require('./utils')
8+
9+
test('YAML file parse', async () => {
10+
const contents = await fs.readFile(path.join(__dirname, './fixtures/simple.yaml'), 'utf-8')
11+
const blocks = parseBlocks(contents, {
12+
syntax: 'yaml',
13+
open: 'GENERATED',
14+
close: 'END-GENERATED',
15+
})
16+
/*
17+
deepLog(blocks.blocks)
18+
/** */
19+
assert.equal(normalizeBlocks(blocks.blocks), [
20+
{
21+
index: 1,
22+
type: 'a',
23+
options: {},
24+
openValue: '## GENERATED a ##\n',
25+
contentValue: '# comment inside',
26+
closeValue: '\n## END-GENERATED ##',
27+
rawArgs: '',
28+
rawContent: '# comment inside',
29+
blockValue: '## GENERATED a ##\n# comment inside\n## END-GENERATED ##'
30+
},
31+
{
32+
index: 2,
33+
type: 'b',
34+
options: {},
35+
openValue: '## GENERATED b ##\n',
36+
contentValue: '# another comment',
37+
closeValue: '\n## END-GENERATED ##',
38+
rawArgs: '',
39+
rawContent: '# another comment',
40+
blockValue: '## GENERATED b ##\n# another comment\n## END-GENERATED ##'
41+
},
42+
{
43+
index: 3,
44+
type: 'c',
45+
options: {},
46+
openValue: '## GENERATED c ##\n',
47+
contentValue: '# multiline\n# comment block',
48+
closeValue: '\n## END-GENERATED ##',
49+
rawArgs: '',
50+
rawContent: '# multiline\n# comment block',
51+
blockValue: '## GENERATED c ##\n# multiline\n# comment block\n## END-GENERATED ##'
52+
},
53+
{
54+
index: 4,
55+
type: 'MyCodeGen',
56+
options: { foo: 'bar' },
57+
openValue: "## GENERATED MyCodeGen foo='bar' ##\n",
58+
contentValue: '# Generated content\nvalue: test',
59+
closeValue: '\n## END-GENERATED ##',
60+
rawArgs: "foo='bar'",
61+
rawContent: '# Generated content\nvalue: test',
62+
blockValue: "## GENERATED MyCodeGen foo='bar' ##\n# Generated content\nvalue: test\n## END-GENERATED ##"
63+
}
64+
])
65+
})
66+
67+
test.run()

0 commit comments

Comments
 (0)