PavelDeuce/backend-project-lvl3

View on GitHub
src/parser.js

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import path from 'path';
import cheerio from 'cheerio';
import entires from 'lodash/entries.js';

import { linkTypesMapping, createLinkPath } from './utils.js';

const tagsMapping = {
  img: 'src',
  link: 'href',
  script: 'src',
};

const changeLinksToRelative = (html, requestPath) => {
  const $ = cheerio.load(html);
  const { origin, host } = new URL(requestPath);
  const fileDirectoryPath = createLinkPath(requestPath, linkTypesMapping.directory);
  const links = [];

  entires(tagsMapping).forEach(([tag, attribute]) => {
    $(tag).each((i, el) => {
      const attr = $(el).attr(attribute);
      if (!attr) return;

      const link = new URL(attr, origin);
      if (link.host !== host) return;
      links.push(link.toString());

      const newPath = path.join(fileDirectoryPath, createLinkPath(link.toString()));
      $(el).attr(attribute, newPath);
    });
  });

  return { links, updatedHtml: $.html() };
};

export default changeLinksToRelative;