summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/markdown_drawer/utils/fetch.js
blob: 7c8e1bc160af5a391eeb00ce02bda4582723cb07 (plain)
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
import * as Sentry from '@sentry/browser';
import { helpPagePath } from '~/helpers/help_page_helper';
import axios from '~/lib/utils/axios_utils';

export const splitDocument = (htmlString) => {
  const htmlDocument = new DOMParser().parseFromString(htmlString, 'text/html');
  const title = htmlDocument.querySelector('h1')?.innerText;
  htmlDocument.querySelector('h1')?.remove();
  return {
    title,
    body: htmlDocument.querySelector('body').innerHTML.toString(),
  };
};

export const getRenderedMarkdown = (documentPath) => {
  return axios
    .get(helpPagePath(documentPath))
    .then(({ data }) => {
      const { body, title } = splitDocument(data.html);
      return {
        body,
        title,
        hasFetchError: false,
      };
    })
    .catch((e) => {
      Sentry.captureException(e);
      return {
        hasFetchError: true,
      };
    });
};