Skip to content

Commit

Permalink
feat: Support .asciidoctorconfig(.adoc) files recursively in folders
Browse files Browse the repository at this point in the history
fixes asciidoctor#380

Signed-off-by: Aurélien Pupier <[email protected]>
  • Loading branch information
apupier committed Oct 28, 2022
1 parent 483ad5e commit e26a55c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Improvements

- support `.asciidoctorconfig` and `.asciidoctorconfig.adoc` at root of the workspace by @apupier, @mogztter and @ahus1 (#380)
- support `.asciidoctorconfig` and `.asciidoctorconfig.adoc` by @apupier, @mogztter and @ahus1 (#380)

### Bug fixes

Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,10 @@ Here's an example of how to use the [asciidoctor-emoji](https://github.com/mogzt

### Asciidoctor Config File

To provide a common set of variables when rendering the preview, the extension reads an `.asciidoctorconfig` configuration file. Use this to optimize the preview when the project contains a document that is split out to multiple include-files.
To provide a common set of variables when rendering the preview, the extension reads an `.asciidoctorconfig` or `.asciidoctorconfig.adoc` configuration file. Use this to optimize the preview when the project contains a document that is split out to multiple include-files.

It is inspired by the implementation provided in [IntelliJ AsciiDoc Plugin](https://intellij-asciidoc-plugin.ahus1.de/docs/users-guide/features/advanced/asciidoctorconfig-file.html) and reused in [Eclipse AsciiDoc plugin](https://github.com/de-jcup/eclipse-asciidoctor-editor/wiki/Asciidoctor-configfiles).

The current scope is limited to an `.asciidoctorconfig` or `.asciidoctorconfig.adoc` configuration file provided at the root of the workspace.

## Extension Settings

This extension contributes the following settings:
Expand Down
3 changes: 3 additions & 0 deletions src/features/asciidoctorConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export async function getAsciidoctorConfigContent (documentUri: vscode.Uri): Pro
if (workspaceFolder === undefined) {
return undefined
}



const configContentFromAsciidoctorConfig = await getConfigContent(workspaceFolder, '.asciidoctorconfig')
const configContentFromAsciidoctorConfigDotAdoc = await getConfigContent(workspaceFolder, '.asciidoctorconfig.adoc')

Expand Down
92 changes: 92 additions & 0 deletions src/test/asciidoctorconfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,96 @@ suite('asciidoc.Asciidoctorconfig', () => {
return configFile
}
})

suite('Pick up asciidocConfig file recursively', async () => {
let html: string
let webview: vscode.WebviewPanel
const createdFiles: vscode.Uri[] = []

suiteSetup(async () => {
console.log('starting setup of second test suite')
webview = vscode.window.createWebviewPanel(
AsciidocPreview.viewType,
'Test',
vscode.ViewColumn.One
)
const root = vscode.workspace.workspaceFolders[0].uri.fsPath
const configFileName = '.asciidoctorconfig'
const rootConfigFile = vscode.Uri.file(`${root}/${configFileName}`)
await vscode.workspace.fs.writeFile(rootConfigFile, Buffer.from(
`:only-root: Only root. Should appear.
:root-and-level1: Value of root-and-level1 specified in root. Should not appear.
:root-and-level1-and-level2: Value of root-and-level1-and-level2 specified in root. Should not appear.`))
createdFiles.push(rootConfigFile)

const level1ConfigFile = vscode.Uri.file(`${root}/level-empty/level1/${configFileName}`)
await vscode.workspace.fs.writeFile(level1ConfigFile, Buffer.from(
`:only-level1: Only level 1. Should appear.
:root-and-level1: Value of root-and-level1 specified in level1. Should appear.
:root-and-level1-and-level2: Value of root-and-level1-and-level2 specified in level1. Should not appear.`))
createdFiles.push(level1ConfigFile)

const level2ConfigFile = vscode.Uri.file(`${root}/level-empty/level1/level2/${configFileName}`)
await vscode.workspace.fs.writeFile(level2ConfigFile, Buffer.from(
`:only-level2: Only level 2. Should appear.
:root-and-level1-and-level2: Value of root-and-level1-and-level2 specified in level2. Should appear.`))
createdFiles.push(level2ConfigFile)

const adocFile = vscode.Uri.file(`${root}/level-empty/level1/level2/fileToTestRecursiveAsciidoctorConfigs.adoc`)
await vscode.workspace.fs.writeFile(adocFile, Buffer.from(
`= {only-root}
= {only-level1}
= {only-level2}
= {root-and-level1}
= {root-and-level1-and-level2}
`))
createdFiles.push(adocFile)

const file = await vscode.workspace.openTextDocument(adocFile)
// eslint-disable-next-line max-len
const asciidocParser = new AsciidocParser(new AsciidocContributionProviderTest(extensionContext.extensionUri), new AsciidoctorExtensionsSecurityPolicyArbiter(extensionContext))
html = (await asciidocParser.convertUsingJavascript(file.getText(), file, extensionContext, webview)).html
})

suiteTeardown(async () => {
webview.dispose()
for (const createdFile of createdFiles) {
await vscode.workspace.fs.delete(createdFile)
}
})

test('Var from root level is substituted', async () => {
assert.strictEqual(
html.includes('>Only root. Should appear.</h1>'), true,
'{only-root} should be substituted by the value defined at root level')
})

test('Var from level1 is substituted', async () => {
assert.strictEqual(
html.includes('>Only level 1. Should appear.</h1>'), true,
'{only-level1} should be substituted by the value defined at level 1')
})

test('Var from level2 is substituted', async () => {
assert.strictEqual(
html.includes('>Only level 2. Should appear.</h1>'), true,
'{only-level2} should be substituted by the value defined at level 2')
})

test('Deepest level should be use to substitue var', async () => {
assert.strictEqual(
html.includes('>Value of root-and-level1-and-level2 specified in level2. Should appear.</h1>'), true,
'{root-and-level1-and-level2} should be substituted by the value defined at level 2')
})

test('Intermediate but deepest level defined should be use to substitue var', async () => {
assert.strictEqual(
html.includes('>Value of root-and-level1 specified in level1. Should appear.</h1>'), true,
'{root-and-level1} should be substituted by the value defined at level 1')
})
})
})

0 comments on commit e26a55c

Please sign in to comment.