Browse Source

Scope Pharos extensions to Asciidoctor extension registry

We were previously registering extensions globally, which impacted every usage of Asciidoctor.js, even those where the extensions were not needed.
master
buttercat1791 11 months ago
parent
commit
613c3a4185
  1. 25
      src/lib/parser.ts

25
src/lib/parser.ts

@ -12,7 +12,7 @@ import type {
} from 'asciidoctor'; } from 'asciidoctor';
import he from 'he'; import he from 'he';
import { writable, type Writable } from 'svelte/store'; import { writable, type Writable } from 'svelte/store';
import { zettelKinds } from './consts'; import { zettelKinds } from './consts.ts';
interface IndexMetadata { interface IndexMetadata {
authors?: string[]; authors?: string[];
@ -66,6 +66,8 @@ export default class Pharos {
private asciidoctor: Asciidoctor; private asciidoctor: Asciidoctor;
private pharosExtensions: Extensions.Registry;
private ndk: NDK; private ndk: NDK;
private contextCounters: Map<string, number> = new Map<string, number>(); private contextCounters: Map<string, number> = new Map<string, number>();
@ -130,25 +132,26 @@ export default class Pharos {
constructor(ndk: NDK) { constructor(ndk: NDK) {
this.asciidoctor = asciidoctor(); this.asciidoctor = asciidoctor();
this.pharosExtensions = this.asciidoctor.Extensions.create();
this.ndk = ndk; this.ndk = ndk;
const pharos = this; const pharos = this;
this.asciidoctor.Extensions.register(function () { this.pharosExtensions.treeProcessor(function () {
const registry = this; const dsl = this;
registry.treeProcessor(function () { dsl.process(function (document) {
const dsl = this; const treeProcessor = this;
dsl.process(function (document) { pharos.treeProcessor(treeProcessor, document);
const treeProcessor = this; });
pharos.treeProcessor(treeProcessor, document);
});
})
}); });
} }
parse(content: string, options?: ProcessorOptions | undefined): void { parse(content: string, options?: ProcessorOptions | undefined): void {
try { try {
this.html = this.asciidoctor.convert(content, options) as string | Document | undefined; this.html = this.asciidoctor.convert(content, {
'extension_registry': this.pharosExtensions,
...options,
}) as string | Document | undefined;
} catch (error) { } catch (error) {
console.error(error); console.error(error);
throw new Error('Failed to parse AsciiDoc document.'); throw new Error('Failed to parse AsciiDoc document.');

Loading…
Cancel
Save