This is an automated email from the ASF dual-hosted git repository.

ShannonDing pushed a commit to branch new-official-website
in repository https://gitbox.apache.org/repos/asf/rocketmq-site.git


The following commit(s) were added to refs/heads/new-official-website by this 
push:
     new dacb0fe7e2 [ISSUE #789] Fix java syntax highlighting (#790)
dacb0fe7e2 is described below

commit dacb0fe7e24491c0905a7b858d27f9c8126fd2c4
Author: mewsf <[email protected]>
AuthorDate: Mon Jun 15 11:30:02 2026 +0800

    [ISSUE #789] Fix java syntax highlighting (#790)
    
    * Attempt to resolve java syntax highlighting
    
    * Attempt to resolve java syntax highlighting
---
 src/theme/prism-include-languages.js |  16 +++++
 src/theme/prism-java.js              | 124 +++++++++++++++++++++++++++++++++++
 2 files changed, 140 insertions(+)

diff --git a/src/theme/prism-include-languages.js 
b/src/theme/prism-include-languages.js
new file mode 100644
index 0000000000..f6dc8f188e
--- /dev/null
+++ b/src/theme/prism-include-languages.js
@@ -0,0 +1,16 @@
+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;
+  require(`./prism-java.js`);
+  delete globalThis.Prism;
+}
diff --git a/src/theme/prism-java.js b/src/theme/prism-java.js
new file mode 100644
index 0000000000..873154cfed
--- /dev/null
+++ b/src/theme/prism-java.js
@@ -0,0 +1,124 @@
+(function (Prism) {
+
+       var keywords = 
/\b(?:abstract|assert|boolean|break|byte|case|catch|char|class|const|continue|default|do|double|else|enum|exports|extends|final|finally|float|for|goto|if|implements|import|instanceof|int|interface|long|module|native|new|non-sealed|null|open|opens|package|permits|private|protected|provides|public|record(?!\s*[(){}[\]<>=%~.:,;?+\-*/&|^])|requires|return|sealed|short|static|strictfp|super|switch|synchronized|this|throw|throws|to|transient|transitive|try|uses|var|void|volatil
 [...]
+
+       // full package (optional) + parent classes (optional)
+       var classNamePrefix = 
/(?:[a-z]\w*\s*\.\s*)*(?:[A-Z]\w*\s*\.\s*)*/.source;
+
+       // based on the java naming conventions
+       var className = {
+               pattern: RegExp(/(^|[^\w.])/.source + classNamePrefix + 
/[A-Z](?:[\d_A-Z]*[a-z]\w*)?\b/.source),
+               lookbehind: true,
+               inside: {
+                       'namespace': {
+                               pattern: 
/^[a-z]\w*(?:\s*\.\s*[a-z]\w*)*(?:\s*\.)?/,
+                               inside: {
+                                       'punctuation': /\./
+                               }
+                       },
+                       'punctuation': /\./
+               }
+       };
+
+       Prism.languages.java = Prism.languages.extend('clike', {
+               'string': {
+                       pattern: /(^|[^\\])"(?:\\.|[^"\\\r\n])*"/,
+                       lookbehind: true,
+                       greedy: true
+               },
+               'class-name': [
+                       className,
+                       {
+                               // variables, parameters, and constructor 
references
+                               // this to support class names (or generic 
parameters) which do not contain a lower case letter (also works for methods)
+                               pattern: RegExp(/(^|[^\w.])/.source + 
classNamePrefix + 
/[A-Z]\w*(?=\s+\w+\s*[;,=()]|\s*(?:\[[\s,]*\]\s*)?::\s*new\b)/.source),
+                               lookbehind: true,
+                               inside: className.inside
+                       },
+                       {
+                               // class names based on keyword
+                               // this to support class names (or generic 
parameters) which do not contain a lower case letter (also works for methods)
+                               pattern: 
RegExp(/(\b(?:class|enum|extends|implements|instanceof|interface|new|record|throws)\s+)/.source
 + classNamePrefix + /[A-Z]\w*\b/.source),
+                               lookbehind: true,
+                               inside: className.inside
+                       }
+               ],
+               'keyword': keywords,
+               'function': [
+                       Prism.languages.clike.function,
+                       {
+                               pattern: /(::\s*)[a-z_]\w*/,
+                               lookbehind: true
+                       }
+               ],
+               'number': 
/\b0b[01][01_]*L?\b|\b0x(?:\.[\da-f_p+-]+|[\da-f_]+(?:\.[\da-f_p+-]+)?)\b|(?:\b\d[\d_]*(?:\.[\d_]*)?|\B\.\d[\d_]*)(?:e[+-]?\d[\d_]*)?[dfl]?/i,
+               'operator': {
+                       pattern: 
/(^|[^.])(?:<<=?|>>>?=?|->|--|\+\+|&&|\|\||::|[?:~]|[-+*/%&|^!=<>]=?)/m,
+                       lookbehind: true
+               },
+               'constant': /\b[A-Z][A-Z_\d]+\b/
+       });
+
+       Prism.languages.insertBefore('java', 'string', {
+               'triple-quoted-string': {
+                       // http://openjdk.java.net/jeps/355#Description
+                       pattern: /"""[ 
\t]*[\r\n](?:(?:"|"")?(?:\\.|[^"\\]))*"""/,
+                       greedy: true,
+                       alias: 'string'
+               },
+               'char': {
+                       pattern: /'(?:\\.|[^'\\\r\n]){1,6}'/,
+                       greedy: true
+               }
+       });
+
+       Prism.languages.insertBefore('java', 'class-name', {
+               'annotation': {
+                       pattern: /(^|[^.])@\w+(?:\s*\.\s*\w+)*/,
+                       lookbehind: true,
+                       alias: 'punctuation'
+               },
+               'generics': {
+                       pattern: 
/<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&)|<(?:[\w\s,.?]|&(?!&))*>)*>)*>)*>/,
+                       inside: {
+                               'class-name': className,
+                               'keyword': keywords,
+                               'punctuation': /[<>(),.:]/,
+                               'operator': /[?&|]/
+                       }
+               },
+               'import': [
+                       {
+                               pattern: RegExp(/(\bimport\s+)/.source + 
classNamePrefix + /(?:[A-Z]\w*|\*)(?=\s*;)/.source),
+                               lookbehind: true,
+                               inside: {
+                                       'namespace': className.inside.namespace,
+                                       'punctuation': /\./,
+                                       'operator': /\*/,
+                                       'class-name': /\w+/
+                               }
+                       },
+                       {
+                               pattern: RegExp(/(\bimport\s+static\s+)/.source 
+ classNamePrefix + /(?:\w+|\*)(?=\s*;)/.source),
+                               lookbehind: true,
+                               alias: 'static',
+                               inside: {
+                                       'namespace': className.inside.namespace,
+                                       'static': /\b\w+$/,
+                                       'punctuation': /\./,
+                                       'operator': /\*/,
+                                       'class-name': /\w+/
+                               }
+                       }
+               ],
+               'namespace': {
+                       pattern: RegExp(
+                               
/(\b(?:exports|import(?:\s+static)?|module|open|opens|package|provides|requires|to|transitive|uses|with)\s+)(?!<keyword>)[a-z]\w*(?:\.[a-z]\w*)*\.?/
+                                       .source.replace(/<keyword>/g, function 
() { return keywords.source; })),
+                       lookbehind: true,
+                       inside: {
+                               'punctuation': /\./,
+                       }
+               }
+       });
+}(Prism));

Reply via email to