summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/static_site_editor/services/renderers/render_image.js
blob: b5651e7163e63898e179f3ab744b7957962cc67b (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
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
import { isAbsolute, getBaseURL, joinPaths } from '~/lib/utils/url_utility';

const canRender = ({ type }) => type === 'image';

let metadata;

const getCachedContent = (basePath) => metadata.imageRepository.get(basePath);

const isRelativeToCurrentDirectory = (basePath) => !basePath.startsWith('/');

const extractSourceDirectory = (url) => {
  const sourceDir = /^(.+)\/([^/]+)$/.exec(url); // Extracts the base path and fileName from an image path
  return sourceDir || [null, null, url]; // If no source directory was extracted it means only a fileName was specified (e.g. url='file.png')
};

const parseCurrentDirectory = (basePath) => {
  const baseUrl = decodeURIComponent(metadata.baseUrl);
  const sourceDirectory = extractSourceDirectory(baseUrl)[1];
  const currentDirectory = sourceDirectory.split(`/-/sse/${metadata.branch}`)[1];

  return joinPaths(currentDirectory, basePath);
};

// For more context around this logic, please see the following comment:
// https://gitlab.com/gitlab-org/gitlab/-/issues/241166#note_409413500
const generateSourceDirectory = (basePath) => {
  let sourceDir = '';
  let defaultSourceDir = '';

  if (!basePath || isRelativeToCurrentDirectory(basePath)) {
    return parseCurrentDirectory(basePath);
  }

  if (!metadata.mounts.length) {
    return basePath;
  }

  metadata.mounts.forEach(({ source, target }) => {
    const hasTarget = target !== '';

    if (hasTarget && basePath.includes(target)) {
      sourceDir = source;
    } else if (!hasTarget) {
      defaultSourceDir = joinPaths(source, basePath);
    }
  });

  return sourceDir || defaultSourceDir;
};

const resolveFullPath = (originalSrc, cachedContent) => {
  if (cachedContent) {
    return `data:image;base64,${cachedContent}`;
  }

  if (isAbsolute(originalSrc)) {
    return originalSrc;
  }

  const sourceDirectory = extractSourceDirectory(originalSrc);
  const [, basePath, fileName] = sourceDirectory;
  const sourceDir = generateSourceDirectory(basePath);

  return joinPaths(getBaseURL(), metadata.project, '/-/raw/', metadata.branch, sourceDir, fileName);
};

const render = ({ destination: originalSrc, firstChild }, { skipChildren }) => {
  skipChildren();

  const cachedContent = getCachedContent(originalSrc);

  return {
    type: 'openTag',
    tagName: 'img',
    selfClose: true,
    attributes: {
      'data-original-src': !isAbsolute(originalSrc) || cachedContent ? originalSrc : '',
      src: resolveFullPath(originalSrc, cachedContent),
      alt: firstChild.literal,
    },
  };
};

const build = (mounts = [], project, branch, baseUrl, imageRepository) => {
  metadata = { mounts, project, branch, baseUrl, imageRepository };
  return { canRender, render };
};

export default { build };