Skip to content

[ISSUE #789] Fix java syntax highlighting - #790

Merged
ShannonDing merged 2 commits into
apache:new-official-websitefrom
mewsf:new-official-website
Jun 15, 2026
Merged

[ISSUE #789] Fix java syntax highlighting#790
ShannonDing merged 2 commits into
apache:new-official-websitefrom
mewsf:new-official-website

Conversation

@mewsf

@mewsf mewsf commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Please do not create a Pull Request without creating an issue first.

What is the purpose of the change

Fix #789

Brief changelog

https://docusaurus.io/docs/2.x/markdown-features/code-blocks#supported-languages has a way to extend language support.

Verifying this change

java_hl_fix

Follow this checklist to help us incorporate your contribution quickly and easily:

  • Make sure there is a Github issue filed for the change (usually before you start working on it). Trivial changes like typos do not require a Github issue. Your pull request should address just this issue, without pulling in other changes - one PR resolves one issue.
  • Format the pull request title like [ISSUE #123] Fix UnknownException when host config not exist. Each commit in the pull request should have a meaningful subject line and body.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit-test to verify your logic correction, more mock a little better when cross module dependency exist. If the new feature or significant change is committed, please remember to add integration-test in test module.
  • Run mvn -B clean apache-rat:check findbugs:findbugs checkstyle:checkstyle to make sure basic checks pass. Run mvn clean install -DskipITs to make sure unit-test pass. Run mvn clean test-compile failsafe:integration-test to make sure integration-test pass.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

@mewsf

mewsf commented Jun 14, 2026

Copy link
Copy Markdown
Contributor Author

依照docusaurus官网的文档,在docusaurus.config.js修改additionalLanguages就能为代码高亮增加更多语言支持, PrimJs也确实支持Java,但是不知什么原因导致一些语言在docusaurus仍然无法支持,推测是这个版本导入依赖时有问题,有些语言的格式化代码没有引入,但是更新docusaurus不太现实。文档中给出了一种手动增加受支持语言的办法,可以通过npm run swizzle @docusaurus/theme-classic prism-include-languages将导入其他语言的功能模块文件手动导出再进行修改。这个文件本身是根据additionalLanguages设置再导入其他语言的格式,内容如下

import siteConfig from '@generated/docusaurus.config';
export default function prismIncludeLanguages(PrismObject) {
  const {
    themeConfig: {prism},
  } = siteConfig;
  const {additionalLanguages} = prism;
  // Prism components work on the Prism instance on the window, while prism-
  // react-renderer uses its own Prism instance. We temporarily mount the
  // instance onto window, import components to enhance it, then remove it to
  // avoid polluting global namespace.
  // You can mutate PrismObject: registering plugins, deleting languages... As
  // long as you don't re-assign it
  globalThis.Prism = PrismObject;
  additionalLanguages.forEach((lang) => {
    // eslint-disable-next-line global-require, import/no-dynamic-require
    require(`prismjs/components/prism-${lang}`);
  });
  delete globalThis.Prism;
}

但是手动导出的文件需要手动引入prismjs模块依赖,为了简洁改为直接将复制粘贴了prism-java.js再导入,方法略微有些dirty但是至少可用。

@ShannonDing
ShannonDing merged commit dacb0fe into apache:new-official-website Jun 15, 2026
1 check passed
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

Successfully merging this pull request may close these issues.

Java syntax highlighting not working

2 participants