Commit 3830bd91 authored by Dan Allen's avatar Dan Allen
Browse files

resolves #976 allow extension to be switched off from CLI by negating value

parent 32709c9d
Loading
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ This project utilizes semantic versioning.

=== Added

* *playbook-builder*: Allow extension to be switched off from CLI by negating value (#976)
* *content-aggregator*: Allow content source to be filtered by commits (playbook model only) (#831)

=== Changed
+1 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
** xref:use-context-variables.adoc[]
** xref:configure-extension.adoc[]
** xref:enable-extension.adoc[]
** xref:switch-off-extension.adoc[]
** xref:extension-helpers.adoc[]
** xref:class-based-extension.adoc[]
** xref:asynchronous-listeners.adoc[]
+28 −0
Original line number Diff line number Diff line
= Switch Off Extension

If an extension is declared as enabled in the playbook file, there is still a way to turn it off for a single run of Antora using the CLI.
Much like the `--extension` option is used to enable an extension flagged as not enabled, the option can also be used to switch the enabled flag to false.

To do so, you negate the value of the `--extension` option by prefixing the value with `!` (read as: not).
After removing the `!`, Antora will look for the first entry whose `id` key matches this value.
If no match is found, Antora will then look for the first entry whose `require` key matches this value.

First, let's give our extension an ID:

.An extension with an ID that is enabled
[,yaml]
----
antora:
  extensions:
  - id: my-extension
    require: ./my-extension.js
    custom: value
----

Now we can now switch off this extension from the CLI as follows:

 $ antora --extension=!my-extension antora-playbook.yml

If a match is found, Antora will switch the the `enabled` key on the extension to `false`, effectively switching it off.

Note that a subsequent `--extension` option could still enable it again.
+10 −12
Original line number Diff line number Diff line
@@ -132,23 +132,21 @@ function registerFormats (convict) {
      const accum = config?.has(name) ? config.get(name) : []
      const byId = {}
      const byRequire = {}
      for (const it of accum) {
        if (it.constructor === Object) {
          byRequire[it.require] ??= it
          if ('id' in it) byId[it.id] ??= it
        } else {
          byRequire[it] = true
        }
      }
      for (const v of new Set(val.split(','))) {
        const match = byId[v] ?? byRequire[v]
      for (const [idx, it] of accum.entries()) {
        const ext = it.constructor === Object ? it : (accum[idx] = { require: it })
        byRequire[ext.require] ??= ext
        if ('id' in ext) byId[ext.id] ??= ext
      }
      for (let request of val.split(',')) {
        const enable = request.charAt() === '!' ? (request = request.slice(1)) == null : true
        const match = byId[request] ?? byRequire[request]
        if (match) {
          if (match !== true && match.enabled === false) match.enabled = true
          if ((match.enabled ?? true) !== enable) match.enabled = enable
        } else {
          accum.push(v)
          accum.push((byRequire[request] = { require: request }))
        }
      }
      return accum
      return accum.map((it) => (Object.keys(it).length === 1 && 'require' in it ? it.require : it))
    },
  })
  convict.addFormat({
+7 −0
Original line number Diff line number Diff line
@@ -596,6 +596,13 @@ describe('buildPlaybook()', () => {
    expect(ext.enabled).to.be.true()
  })

  it('should set enabled flag on entry in require-array type from playbook file to false if option begins with !', () => {
    const args = ['--playbook', defaultSchemaSpec, '--extension', '!pdf-exporter']
    const playbook = buildPlaybook(args, {})
    const ext = playbook.antora.extensions.find((it) => it.id === 'pdf-exporter')
    expect(ext.enabled).to.be.false()
  })

  it('should set enabled flag on first entry in require-array type from playbook file that matches id', () => {
    const args = ['--playbook', duplicateExtensionIdSchemaSpec, '--extension', '@antora/pdf-extension']
    const playbook = buildPlaybook(args, {})