All files index.ts

100% Statements 26/26
90.69% Branches 39/43
100% Functions 5/5
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112                                                                                                                        1x 58x 58x 58x 110x 14x 14x 14x 14x 10x 10x 9x 9x         110x 110x 110x 94x 94x 3x 12x 4x                                 98x 98x 48x 48x 45x            
import type { Plugin } from 'unified';
import type { Root, Element, Comment, Properties, Literal, RootContent, ElementContent } from 'hast';
import { visit } from 'unist-util-visit';
import { propertiesHandle, nextChild, prevChild, getCommentObject } from './utils.js';
 
export type RehypeAttrsOptions = {
  /**
   * ## `data`
   * 
   * ```markdown
   * text
   * <!--rehype:title=Rehype Attrs&abc=2-->
   * ```
   * 👇👇👇👇👇
   * ```html
   * <p data-config="data-config='[object Object]'">text</p>
   * ```
   * 
   * ## `string`
   * 
   * ```markdown
   * text
   * <!--rehype:title=Rehype Attrs-->
   * ```
   * 
   * 👇👇👇👇👇
   * 
   * ```html
   * <p data-config="{&#x22;title&#x22;:&#x22;Rehype Attrs&#x22;,&#x22;rehyp&#x22;:true}">text</p>
   * ```
   * 
   * ## attr
   * 
   * ```markdown
   * text
   * <!--rehype:title=Rehype Attrs-->
   * ```
   * 👇👇👇👇👇
   * ```html
   * <p title="Rehype Attrs">text</p>
   * ```
   * @default `data`
   */
  properties?: 'data' | 'string' | 'attr';
  /**
   * Code block passing parameters
   */
  codeBlockParames?: boolean;
  /** 
   * Optional start delimiter for comments @example `\\{\\*`
   * @default `<!--`
   */
  commentStart?: string;
  /**
   * Optional end delimiter for comments @example `\\*\\}` 
   * @default `-->`
   */
  commentEnd?: string;
}
 
const rehypeAttrs: Plugin<[RehypeAttrsOptions?], Root> = (options = {}) => {
  const { properties = 'data', codeBlockParames = true, commentStart = "<!--", commentEnd = "-->" } = options;
  return (tree) => {
    visit(tree, 'element', (node, index, parent) => {
      if (codeBlockParames && node.tagName === 'pre' && node && Array.isArray(node.children) && parent && Array.isArray(parent.children) && parent.children.length > 1) {
        const firstChild = node.children[0] as Element;
        Eif (firstChild && firstChild.tagName === 'code' && typeof index === 'number') {
          const child = prevChild(parent.children as Literal[], index);
          if (child) {
            const attr = getCommentObject(child, commentStart, commentEnd);
            if (Object.keys(attr).length > 0) {
              node.properties = { ...node.properties, ...{ 'data-type': 'rehyp' } }
              firstChild.properties = propertiesHandle(firstChild.properties, attr, properties) as Properties
            }
          }
        }
      }
      let rootnode = parent as Root
      let testTagName = /^(em|strong|b|a|i|p|pre|kbd|blockquote|h(1|2|3|4|5|6)|code|table|img|del|ul|ol|li)$/.test(node.tagName)
      if ((testTagName || rootnode.type == "root") && parent && Array.isArray(parent.children) && typeof index === 'number') {
        addPropertyToNode(node, parent.children, index, properties, commentStart, commentEnd)
        if (node.tagName == "ul") {
          node.children.forEach((li, _) => {
            if (li.type == "element" && li.tagName == "li") {
              addPropertyToNode(li, li.children, 0, properties, commentStart, commentEnd)
            }
          })
        }
      }
    });
  }
}
 
function addPropertyToNode(
  node: Element, 
  children: RootContent[] | ElementContent[] = [], 
  index: number, 
  properties: RehypeAttrsOptions['properties'], 
  commentStart = "<!--", 
  commentEnd = "-->"
) {
  const child = nextChild(children, index, "", commentStart, commentEnd)
  if (child) {
    const attr = getCommentObject(child as Comment, commentStart, commentEnd)
    if (Object.keys(attr).length > 0) {
      node.properties = propertiesHandle(node.properties, attr, properties) as Properties
    }
  }
}
 
export default rehypeAttrs