Passed
Pull Request — master (#22)
by Vitor
02:22
created

src/functions/recursiveMarkdownRender.js   A

Complexity

Total Complexity 5
Complexity/F 5

Size

Lines of Code 30
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 30
bpm 4
cpm 5
noi 0
c 0
b 0
f 0
rs 10
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
      if (isObject(value)) {
15
        obj[property] = recursiveMarkdownRender(value);
16
      } else if (isString(value) && value.startsWith(markdownPreface)) {
17
        // value must be a string and not start with a period (look like a path)
18
        const html = remark()
19
          .use(remarkHTML)
20
          .processSync(value.split(markdownPreface).join(''))
21
          .toString();
22
23
        obj[property] = html;
24
      }
25
    }
26
  }
27
28
  return obj;
29
};
30
31
export { recursiveMarkdownRender };
32