src/functions/recursiveMarkdownRender.js   A
last analyzed

Complexity

Total Complexity 5
Complexity/F 5

Size

Lines of Code 31
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 19
mnd 4
bc 4
fnc 1
dl 0
loc 31
rs 10
bpm 4
cpm 5
noi 0
c 0
b 0
f 0
1
/* eslint-disable no-param-reassign */
2
import remark from 'remark';
3
import remarkHTML from 'remark-html';
4
import isString from 'lodash/isString';
5
import isObject from 'lodash/isObject';
6
7
const markdownPreface = 'md//';
8
9
const recursiveMarkdownRender = (obj) => {
10
  // eslint-disable-next-line no-restricted-syntax
11
  for (const property in obj) {
12
    if (Object.prototype.hasOwnProperty.call(obj, property)) {
13
      const value = obj[property];
14
15
      if (isObject(value)) {
16
        obj[property] = recursiveMarkdownRender(value);
17
      } else if (isString(value) && value.startsWith(markdownPreface)) {
18
        // value must be a string and not start with a period (look like a path)
19
        const html = remark()
20
          .use(remarkHTML)
21
          .processSync(value.split(markdownPreface).join(''))
22
          .toString();
23
24
        obj[property] = html;
25
      }
26
    }
27
  }
28
29
  return obj;
30
};
31
32
export { recursiveMarkdownRender };
33