Welcome to mirror list, hosted at ThFree Co, Russian Federation.

print_markdown_dom.js « lib « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fb5ea09b6c8341b77703aa0f3c3953126453d3b7 (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function getPrintContent(target, ignoreSelectors) {
  const cloneDom = target.cloneNode(true);
  cloneDom.querySelectorAll('details').forEach((detail) => {
    detail.setAttribute('open', '');
  });

  if (Array.isArray(ignoreSelectors) && ignoreSelectors.length > 0) {
    cloneDom.querySelectorAll(ignoreSelectors.join(',')).forEach((ignoredNode) => {
      ignoredNode.remove();
    });
  }

  cloneDom.querySelectorAll('img').forEach((img) => {
    img.setAttribute('loading', 'eager');
  });

  return cloneDom.innerHTML;
}

function getTitleContent(title) {
  const titleElement = document.createElement('h2');
  titleElement.className = 'gl-mt-0 gl-mb-5';
  titleElement.innerText = title;
  return titleElement.outerHTML;
}

export default async function printMarkdownDom({
  target,
  title,
  ignoreSelectors = [],
  stylesheet = [],
}) {
  const printJS = (await import('print-js')).default;

  const printContent = getPrintContent(target, ignoreSelectors);

  const titleElement = title ? getTitleContent(title) : '';

  const markdownElement = `<div class="md">${printContent}</div>`;

  const printable = titleElement + markdownElement;

  printJS({
    printable,
    type: 'raw-html',
    documentTitle: title,
    scanStyles: false,
    css: stylesheet,
  });
}