Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Whitelisting domains? #5

Closed
zslabs opened this issue Oct 25, 2018 · 3 comments
Closed

Whitelisting domains? #5

zslabs opened this issue Oct 25, 2018 · 3 comments
Labels
👀 no/external This makes more sense somewhere else

Comments

@zslabs
Copy link
Contributor

zslabs commented Oct 25, 2018

👋 Thanks for creating this! I work with a team that creates content on a fairly regular basis; and unfortunately it'd be easier said then done to tell them to use just relative links for their internal links, but most do use a fully qualified domain; which at the moment - the regex sees as an external one. Any opinion on if you think that'd be a good feature to add?

@wooorm
Copy link
Member

wooorm commented Oct 26, 2018

Hmm, interesting! You could also create a plugin that “relativises” URLs based on a given domain! Something like this plugin, but rewriting the URLs based on a base url?

@zslabs
Copy link
Contributor Author

zslabs commented Oct 26, 2018

@wooorm Good idea! Until I publish a repo, here's what I ended up using (for anyone else reading this later; I put this in before remark-external-links:

const visit = require('unist-util-visit');

module.exports = (options = {}) => {
  if (!options.domainRegex) {
    throw Error('Missing required "domainRegex" option');
  }

  function visitor(node) {
    if (options.domainRegex.test(node.url)) {
      node.url = node.url.replace(options.domainRegex, '/');
    }
  }

  function transform(tree) {
    visit(tree, 'link', visitor);
    visit(tree, 'linkReference', visitor);
  }

  return transform;
};

Example regex: /http[s]*:\/\/[www.]*google\.com[/]?/

@zslabs
Copy link
Contributor Author

zslabs commented Oct 26, 2018

Created https://github.com/zslabs/remark-relative-links

@zslabs zslabs closed this as completed Oct 26, 2018
zslabs added a commit to zslabs/remark that referenced this issue Oct 26, 2018
@wooorm wooorm added the 👀 no/external This makes more sense somewhere else label Aug 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
👀 no/external This makes more sense somewhere else
Development

No branches or pull requests

2 participants