Просмотр исходного кода

refactor addLineNumberAttribute

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

+ 8 - 1
packages/app/src/services/renderer/rehype-plugins/add-line-number-attribute.ts

@@ -1,10 +1,11 @@
+import { Schema as SanitizeOption } from 'hast-util-sanitize';
 import { Element } from 'hast-util-select';
 import { Element } from 'hast-util-select';
 import { Plugin } from 'unified';
 import { Plugin } from 'unified';
 import { visit } from 'unist-util-visit';
 import { visit } from 'unist-util-visit';
 
 
 const REGEXP_TARGET_TAGNAMES = new RegExp(/h1|h2|h3|h4|h5|h6|p|img|pre|blockquote|hr|ol|ul/);
 const REGEXP_TARGET_TAGNAMES = new RegExp(/h1|h2|h3|h4|h5|h6|p|img|pre|blockquote|hr|ol|ul/);
 
 
-export const addLineNumberAttribute: Plugin = () => {
+export const rehypePlugin: Plugin = () => {
   return (tree) => {
   return (tree) => {
     visit(tree, 'element', (node: Element) => {
     visit(tree, 'element', (node: Element) => {
       if (REGEXP_TARGET_TAGNAMES.test(node.tagName as string)) {
       if (REGEXP_TARGET_TAGNAMES.test(node.tagName as string)) {
@@ -20,3 +21,9 @@ export const addLineNumberAttribute: Plugin = () => {
     });
     });
   };
   };
 };
 };
+
+export const sanitizeOption: SanitizeOption = {
+  attributes: {
+    '*': ['data-line'],
+  },
+};

+ 3 - 7
packages/app/src/services/renderer/renderer.tsx

@@ -28,7 +28,7 @@ import { RendererConfig } from '~/interfaces/services/renderer';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
 import { addClass } from './rehype-plugins/add-class';
 import { addClass } from './rehype-plugins/add-class';
-import { addLineNumberAttribute } from './rehype-plugins/add-line-number-attribute';
+import * as addLineNumberAttribute from './rehype-plugins/add-line-number-attribute';
 import { relativeLinks } from './rehype-plugins/relative-links';
 import { relativeLinks } from './rehype-plugins/relative-links';
 import { relativeLinksByPukiwikiLikeLinker } from './rehype-plugins/relative-links-by-pukiwiki-like-linker';
 import { relativeLinksByPukiwikiLikeLinker } from './rehype-plugins/relative-links-by-pukiwiki-like-linker';
 import { pukiwikiLikeLinker } from './remark-plugins/pukiwiki-like-linker';
 import { pukiwikiLikeLinker } from './remark-plugins/pukiwiki-like-linker';
@@ -439,7 +439,7 @@ export const generatePreviewOptions = (pagePath: string, config: RendererConfig)
   rehypePlugins.push(
   rehypePlugins.push(
     katex,
     katex,
     [lsxGrowiPlugin.rehypePlugin, { pagePath }],
     [lsxGrowiPlugin.rehypePlugin, { pagePath }],
-    addLineNumberAttribute,
+    addLineNumberAttribute.rehypePlugin,
     // [autoLinkHeadings, {
     // [autoLinkHeadings, {
     //   behavior: 'append',
     //   behavior: 'append',
     // }]
     // }]
@@ -448,11 +448,7 @@ export const generatePreviewOptions = (pagePath: string, config: RendererConfig)
   const sanitizeOption = deepmerge(
   const sanitizeOption = deepmerge(
     commonSanitizeOption,
     commonSanitizeOption,
     lsxGrowiPlugin.sanitizeOption,
     lsxGrowiPlugin.sanitizeOption,
-    {
-      attributes: {
-        '*': ['data-line'],
-      },
-    },
+    addLineNumberAttribute.sanitizeOption,
   );
   );
   rehypePlugins.push([sanitize, sanitizeOption]);
   rehypePlugins.push([sanitize, sanitizeOption]);