Skip to content

Support multi-line yaml to properties conversion #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/configuration-properties-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ class YamlToPropertiesConverter {
if (type === Array) {
return val.reduce((accum, it, idx) => accum.concat(this.toProperty(`${key}[${idx}]`, it, prefix, true)), [])
}
if ((typeof val === 'string' || val instanceof String) && val.includes('\n')) {
const lines = val.split('\n').slice(0, -1)
const lastIdx = lines.length - 1
val = lines.reduce((accum, it, idx) => {
accum = accum.concat(`${it}\\n`)
if (idx !== lastIdx) accum = accum.concat('\\\n')
if (idx === 0) accum = '\\\n'.concat(accum)
return accum
}, '')
}
return [`${propertyName}=${val}`]
}
}
Expand Down
4 changes: 3 additions & 1 deletion lib/include-code-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ function createExtensionGroup (context) {
if (!langs.length) return log(doc, 'warn', `no search locations defined for include-code::${target}[]`)
const cursor = doc.getReader().$cursor_at_mark()
tabsEnabled ??= doc.getExtensions().hasBlocks() && !!doc.getExtensions().getBlockFor('tabs', 'example')
const attrsStr = Object.entries(attrs).filter(([n, v]) => n !== 'title').reduce((buf, [n, v]) => `${buf}${buf ? ',' : ''}${n}=${v}`, '')
const attrsStr = Object.entries(attrs)
.filter(([n, v]) => n !== 'title')
.reduce((buf, [n, v]) => `${buf}${buf ? ',' : ''}${n}=${v}`, '')
const sectionId = (nearest(parent, 'section') || doc).getId()
const relativeIdPath = sectionId
? sectionId.replaceAll('-', '').replaceAll('.', '/') + '/' + sanitizedTarget
Expand Down
20 changes: 20 additions & 0 deletions test/configuration-properties-extension-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,26 @@ describe('configuration-properties-extension', () => {
expect(propertiesBlock.getSourceLines()).to.eql(expected)
})

it('should convert multi-line property in configprops,yaml block to properties', () => {
addConfigurationMetadataFixture()
const input = heredoc`
[configprops,yaml]
----
foo:
bar:
baz: |
one
two
three
----
`
expect(messages).to.be.empty()
const actual = run(input)
const propertiesBlock = actual.findBy({ context: 'listing' })[0]
const expected = ['foo.bar.baz=\\', 'one\\n\\', 'two\\n\\', 'three\\n']
expect(propertiesBlock.getSourceLines()).to.eql(expected)
})

it('should only warn once if invalid property is used in multiple documents in configprops,yaml block', () => {
const input = heredoc`
[configprops,yaml]
Expand Down
7 changes: 6 additions & 1 deletion test/include-code-extension-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,12 @@ describe('include-code-extension', () => {
const codeBlocks = tabs.findBy({ context: 'listing' })
expect(codeBlocks).to.have.lengthOf(4)
const actualProperties = codeBlocks.map((block) => {
return { style: block.getStyle(), language: block.getAttributes().language, title: block.getTitle(), someAttribute: block.getAttribute('some-attribute') }
return {
style: block.getStyle(),
language: block.getAttributes().language,
title: block.getTitle(),
someAttribute: block.getAttribute('some-attribute'),
}
})
console.log(actualProperties)
expect(actualProperties).to.eql(expected)
Expand Down