codeblock.ts 677 B

1234567891011121314151617181920212223
  1. import type { Schema as SanitizeOption } from 'hast-util-sanitize';
  2. import type { InlineCode } from 'mdast';
  3. import type { Plugin } from 'unified';
  4. import { visit } from 'unist-util-visit';
  5. const SUPPORTED_CODE = ['inline'];
  6. export const remarkPlugin: Plugin = () => {
  7. return (tree) => {
  8. visit(tree, 'inlineCode', (node: InlineCode) => {
  9. const data = node.data || (node.data = {});
  10. data.hProperties = { inline: 'true' }; // set 'true' explicitly because the empty string is evaluated as false for `if (inline) { ... }`
  11. });
  12. };
  13. };
  14. export const sanitizeOption: SanitizeOption = {
  15. tagNames: ['code'],
  16. attributes: {
  17. code: SUPPORTED_CODE,
  18. },
  19. };