Yuki Takei 3 лет назад
Родитель
Сommit
bd454411d3

+ 30 - 0
packages/app/src/services/renderer/growi-plugins/lsx.ts

@@ -0,0 +1,30 @@
+import { RemarkGrowiPluginType } from '@growi/remark-growi-plugin';
+import { Plugin } from 'unified';
+import { visit } from 'unist-util-visit';
+
+const NODE_NAME_PATTERN = new RegExp(/ls|lsx/);
+
+
+type DirectiveAttributes = Record<string, string>
+
+
+export const remarkPlugin: Plugin = function() {
+  return (tree) => {
+    visit(tree, (node) => {
+      if (node.type === RemarkGrowiPluginType.Text || node.type === RemarkGrowiPluginType.Leaf) {
+        if (typeof node.name !== 'string') {
+          return;
+        }
+        if (!NODE_NAME_PATTERN.test(node.name)) {
+          return;
+        }
+
+        const data = node.data ?? (node.data = {});
+        const attributes = node.attributes as DirectiveAttributes || {};
+
+        data.hName = 'lsx';
+        data.hProperties = attributes;
+      }
+    });
+  };
+};

+ 7 - 0
packages/app/src/services/renderer/renderer.ts

@@ -17,6 +17,7 @@ import { NextLink } from '~/components/ReactMarkdownComponents/NextLink';
 import { RendererConfig } from '~/interfaces/services/renderer';
 import loggerFactory from '~/utils/logger';
 
+import * as lsxGrowiPlugin from './growi-plugins/lsx';
 import { addClass } from './rehype-plugins/add-class';
 import { relativeLinks } from './rehype-plugins/relative-links';
 import { relativeLinksByPukiwikiLikeLinker } from './rehype-plugins/relative-links-by-pukiwiki-like-linker';
@@ -233,6 +234,11 @@ const generateCommonOptions = (pagePath: string|undefined, config: RendererConfi
       raw,
       [sanitize, {
         ...sanitizeDefaultSchema,
+        tagNames: [
+          ...(sanitizeDefaultSchema.tagNames ?? []),
+          'ls',
+          'lsx',
+        ],
         attributes: {
           ...sanitizeDefaultSchema.attributes,
           '*': sanitizeDefaultSchema.attributes != null
@@ -268,6 +274,7 @@ export const generateViewOptions = (
     if (config.isEnabledLinebreaks) {
       remarkPlugins.push(breaks);
     }
+    remarkPlugins.push(lsxGrowiPlugin.remarkPlugin);
   }
 
   // store toc node