Skip to content
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

Unable to load custom FormatterExtension #579

Open
SageJustus opened this issue Jun 13, 2023 · 0 comments
Open

Unable to load custom FormatterExtension #579

SageJustus opened this issue Jun 13, 2023 · 0 comments

Comments

@SageJustus
Copy link

I follow CustomLinkResolverSample.java example and Render AST as Markdown with Formatting Options to implement custom parsing link, but not working.

fun main() {
    val str = "![cover](https://www.google.com)"
    val options = PegdownOptionsAdapter.flexmarkOptions(
        Extensions.ALL
    )
    val parser = Parser.builder(options).build()
    val document = parser.parse(str)
    val formatOptions = MutableDataSet()
        .set(Parser.EXTENSIONS, Parser.EXTENSIONS.get(options))
        .set(
            Parser.EXTENSIONS,
            Collections.singletonList(SageFormatterExtension.create()) as Collection<Extension>
        )
    val render = Formatter.builder(formatOptions).build()
    val result = render.render(document)
    println(result)
}

SageFormatterExtension

class SageFormatterExtension: FormatterExtension {
    override fun rendererOptions(options: MutableDataHolder?) {
    }

    override fun extend(formatterBuilder: Formatter.Builder) {
        formatterBuilder.linkResolverFactory(DocxLinkResolver.Factory)
    }

    companion object {
        fun create(): SageFormatterExtension {
            return SageFormatterExtension()
        }
    }
}

DocxLinkResolver

class DocxLinkResolver(val context: LinkResolverBasicContext) : LinkResolver {

    override fun resolveLink(node: Node, context: LinkResolverBasicContext, link: ResolvedLink): ResolvedLink {
        println("resolveLink")
        return link
    }

    object Factory : LinkResolverFactory {
        override fun apply(context: LinkResolverBasicContext): LinkResolver {
            return DocxLinkResolver(context)
        }

        override fun getAfterDependents(): MutableSet<Class<*>>? {
            return null
        }

        override fun getBeforeDependents(): MutableSet<Class<*>>? {
            return null
        }

        override fun affectsGlobalScope(): Boolean {
            return false
        }

    }

}

println("resolveLink") did not run, indicating that the extension was not loaded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant