How to use the mdurl.parse function in mdurl

To help you get started, we’ve selected a few mdurl examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nodeca / url-unshort / lib / index.js View on Github external
let url_normalized = mdurl.format(u);

    //
    // At top level try cache first. On recursive calls skip cache.
    // !! Cache should be probed even for disabled services, to resolve old links.
    //
    let result;

    if (nestingLeft === this.nesting) {
      result = await this.cache.get(url_normalized);

      // If cache exists - use it.
      if (result || result === null) {
        // forward hash if needed
        if (hash && result) {
          u = mdurl.parse(result, true);
          u.hash = u.hash || hash;
          result = mdurl.format(u);
        }

        return result;
      }
    }

    //
    // First pass validation (quick).
    //

    if (!this._matchAllRE.test(url_normalized)) break;

    // Something found - run additional checks.
github christianvoigt / argdown / packages / argdown-parser / lib / src / utils.js View on Github external
function normalizeLinkText(url) {
  var parsed = mdurl.parse(url, true);

  if (parsed.hostname) {
    // Encode hostnames in urls like:
    // `http://host/`, `https://host/`, `mailto:user@host`, `//host/`
    //
    // We don't encode unknown schemas, because it's likely that we encode
    // something we shouldn't (e.g. `skype:name` treated as `skype:host`)
    //
    if (!parsed.protocol || RECODE_HOSTNAME_FOR.indexOf(parsed.protocol) >= 0) {
      try {
        parsed.hostname = punycode.toUnicode(parsed.hostname);
      } catch (er) {/**/}
    }
  }

  return mdurl.decode(mdurl.format(parsed));
github nodeca / url-unshort / lib / index.js View on Github external
let domainConfig = this._domains.find(dc => dc._compiledMatch.exec(url_normalized));

    if (!domainConfig || !domainConfig.validate(url_normalized)) break;

    // Valid redirector => should cache result
    shouldCache = true;


    result = await domainConfig.fetch.call(this, url_normalized, domainConfig);

    // If unshortener has persistent fail - stop.
    if (!result) break;

    // Parse and check url
    //
    u = mdurl.parse(result, true);

    if (!u.hostname) {
      // relative urls are not supported for now
      throw new UnshortError('Redirected to an invalid location', 'EBADREDIRECT');
    }

    if (u.protocol && u.protocol !== 'http:' && u.protocol !== 'https:') {
      // Accept:
      //
      //  - http:// protocol (e.g. http://example.org/)
      //  - https:// protocol (e.g. https://example.org/)
      //  - protocol-relative links (e.g. //example.org/)
      //
      // Restriction is done for security reasons. Even though browsers
      // can redirect anywhere, most shorteners have similar restrictions.
      //
github christianvoigt / argdown / packages / argdown-parser / lib / src / utils.js View on Github external
function normalizeLink(url) {
  var parsed = mdurl.parse(url, true);

  if (parsed.hostname) {
    // Encode hostnames in urls like:
    // `http://host/`, `https://host/`, `mailto:user@host`, `//host/`
    //
    // We don't encode unknown schemas, because it's likely that we encode
    // something we shouldn't (e.g. `skype:name` treated as `skype:host`)
    //
    if (!parsed.protocol || RECODE_HOSTNAME_FOR.indexOf(parsed.protocol) >= 0) {
      try {
        parsed.hostname = punycode.toASCII(parsed.hostname);
      } catch (er) {/**/}
    }
  }

  return mdurl.encode(mdurl.format(parsed));
github nodeca / nodeca.core / lib / parser / beautify_url.js View on Github external
function beautify_url(url_str, max_length) {
  if (typeof url_str === 'undefined' || url_str === null) return '';

  var url = mdurl.parse(String(url_str), true);

  // urls without host and protocol, e.g. "example.org/foo"
  if (!url.protocol && !url.slashes && !url.hostname) {
    url = mdurl.parse('//' + url_str, true);
  }

  try {
    if (url.hostname) {
      url.hostname = punycode.toUnicode(url.hostname);
    }
  } catch (e) {}

  // Decode url-encoded characters
  //
  if (url.auth)     url.auth     = mdurl.decode(url.auth);
  if (url.hash)     url.hash     = mdurl.decode(url.hash);
  if (url.search)   url.search   = mdurl.decode(url.search);
  if (url.pathname) url.pathname = mdurl.decode(url.pathname);

  // Omit protocol if it's http, https or mailto
github MarkBind / markbind / src / lib / markbind / src / lib / markdown-it / normalizeLink.js View on Github external
function normalizeLink(url) {
  var parsed = mdurl.parse(url, true);

  if (parsed.hostname) {
    // Encode hostnames in urls like:
    // `http://host/`, `https://host/`, `mailto:user@host`, `//host/`
    //
    // We don't encode unknown schemas, because it's likely that we encode
    // something we shouldn't (e.g. `skype:name` treated as `skype:host`)
    //
    if (!parsed.protocol || RECODE_HOSTNAME_FOR.indexOf(parsed.protocol) >= 0) {
      try {
        parsed.hostname = punycode.toASCII(parsed.hostname);
      } catch (er) { /**/ }
    }
  }

  return mdurl.encode(mdurl.format(parsed));
github markdown-it / markdown-it / lib / index.js View on Github external
function normalizeLink(url) {
  var parsed = mdurl.parse(url, true);

  if (parsed.hostname) {
    // Encode hostnames in urls like:
    // `http://host/`, `https://host/`, `mailto:user@host`, `//host/`
    //
    // We don't encode unknown schemas, because it's likely that we encode
    // something we shouldn't (e.g. `skype:name` treated as `skype:host`)
    //
    if (!parsed.protocol || RECODE_HOSTNAME_FOR.indexOf(parsed.protocol) >= 0) {
      try {
        parsed.hostname = punycode.toASCII(parsed.hostname);
      } catch (er) { /**/ }
    }
  }

  return mdurl.encode(mdurl.format(parsed));
github christianvoigt / argdown / packages / argdown-core / src / utils.ts View on Github external
export const normalizeLinkText = (url: string): string => {
  var parsed = mdurl.parse(url, true);

  if (parsed.hostname) {
    // Encode hostnames in urls like:
    // `http://host/`, `https://host/`, `mailto:user@host`, `//host/`
    //
    // We don't encode unknown schemas, because it's likely that we encode
    // something we shouldn't (e.g. `skype:name` treated as `skype:host`)
    //
    if (!parsed.protocol || RECODE_HOSTNAME_FOR.indexOf(parsed.protocol) >= 0) {
      try {
        parsed.hostname = punycode.toUnicode(parsed.hostname);
      } catch (er) {
        /**/
      }
    }
  }
github nodeca / url-unshort / lib / providers / flic.kr.js View on Github external
try {
      res = await this.request(url, { method: 'HEAD' });
    } catch (e) {

      if (e.statusCode >= 400 && e.statusCode < 500) return null;

      throw new UnshortError(
        `Remote server error, code ${e.code}, statusCode ${e.statusCode}`,
        'EHTTP',
        e.statusCode);
    }

    if (isRedirect(res.statusCode)) {
      let uSrc = mdurl.parse(url, true);
      let uDst = mdurl.parse(res.headers.location, true);

      if (!uDst.hostname) { uDst.hostname = uSrc.hostname; }
      if (!uDst.protocol) { uDst.protocol = uSrc.protocol; }
      if (!uDst.slashes)  { uDst.slashes  = uSrc.slashes; }

      url = mdurl.format(uDst);
      continue;
    }

    // reached destination
    if (res.statusCode >= 200 && res.statusCode < 300) return url;

    throw new UnshortError(`Unexpected status code: ${res.statusCode}`, 'EHTTP', res.statusCode);
  }

  return null;
github petrosagg / papagal / src / vendor / markdown-it / lib / index.js View on Github external
function o(e) {
    var t = f.parse(e, true);
    if (t.hostname && (!t.protocol || y.indexOf(t.protocol) >= 0)) {
        try {
            t.hostname = m.toASCII(t.hostname);
        } catch (n) {}
    }
    return f.encode(f.format(t));
}

mdurl

URL utilities for markdown-it

MIT
Latest version published 8 months ago

Package Health Score

70 / 100
Full package analysis

Similar packages