Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

951 B
Raw Blame
export function rehypePrefixBaseToRootLinks(options = {}) {
  const base = normalizeBase(options.base);

  return (tree) => {
    if (!base) {
      return;
    }

    walk(tree, (node) => {
      if (node.tagName !== 'a') {
        return;
      }

      const href = node.properties?.href;

      if (typeof href !== 'string') {
        return;
      }

      if (!href.startsWith('/') || href.startsWith('//') || href.startsWith(`${base}/`)) {
        return;
      }

      node.properties.href = `${base}${href}`;
    });
  };
}

function walk(node, visitor) {
  if (!node || typeof node !== 'object') {
    return;
  }

  if (node.type === 'element') {
    visitor(node);
  }

  if (!Array.isArray(node.children)) {
    return;
  }

  for (const child of node.children) {
    walk(child, visitor);
  }
}

function normalizeBase(base) {
  if (!base || base === '/') {
    return '';
  }

  return base.endsWith('/') ? base.slice(0, -1) : base;
}