documents/marp/node_modules/@marp-team/marpit/lib/markdown/image/parse.js
/**
 * Marpit image parse plugin.
 *
 * Parse image tokens and store the result into `marpitImage` meta. It has an
 * image url and options. The alternative text is regarded as space-separated
 * options.
 *
 * @alias module:markdown/image/parse
 * @param {MarkdownIt} md markdown-it instance.
 */
Marpit image parse plugin.
Parse image tokens and store the result into `marpitImage` meta. It has an
image url and options. The alternative text is regarded as space-separated
options.

@alias module:markdown/image/parse
@param {MarkdownIt} md markdown-it instance.
Marpit image parse plugin . Parse image tokens and store the result into ` marpitImage ` meta . It has an image url and options . The alternative text is regarded as space - separated options . @alias module : markdown / image / parse @param { MarkdownIt } md markdown - it instance .
function parseImage(md) {
  const {
    process
  } = md.core; 
  let originalURLMap;
  let refCount = 0;
  const finalizeTokenAttr = token => {
    if (token.attrs && Array.isArray(token.attrs)) token.attrs = token.attrs.map(([name, value]) => [name, value.toString()]);
    if (token.type === 'inline') {
      for (const t of token.children) finalizeTokenAttr(t);
    }
  };
  md.core.process = state => {
    const {
      normalizeLink
    } = md; 
    if (refCount === 0) originalURLMap = new WeakMap();
    try {
      md.normalizeLink = url => {
        const imprimitiveUrl = new String(normalizeLink.call(md, url));
        originalURLMap.set(imprimitiveUrl, url);
        return imprimitiveUrl;
      };
      refCount += 1;
      return process.call(md.core, state);
    } finally {
      refCount -= 1;
      md.normalizeLink = normalizeLink;
      if (refCount === 0) {
        for (const token of state.tokens) finalizeTokenAttr(token);
      }
    }
  };
  md.inline.ruler2.push('marpit_parse_image', ({
    tokens
  }) => {
    for (const token of tokens) {
      if (token.type === 'image') {
        const options = token.content.split(/\s+/).filter(s => s.length > 0);
        const url = token.attrGet('src');
        const originalUrl = originalURLMap.has(url) ? originalURLMap.get(url) : url;
        token.meta = token.meta || {};
        token.meta.marpitImage = _objectSpread(_objectSpread({}, token.meta.marpitImage || {}), {}, {
          url: url.toString(),
          options
        }); 
        if (!!_colorString.default.get(originalUrl) || originalUrl.toLowerCase() === 'currentcolor') {
          token.meta.marpitImage.color = originalUrl;
          token.hidden = true;
        } 
        for (const opt of options) {
          for (const [regexp, mergeFunc] of optionMatchers) {
            const matched = opt.match(regexp);
            if (matched) token.meta.marpitImage = _objectSpread(_objectSpread({}, token.meta.marpitImage), mergeFunc(matched, _objectSpread({
              filters: []
            }, token.meta.marpitImage)));
          }
        }
      }
    }
  });
}
function parseImage ( md ) { const { process } = md . core ; let originalURLMap ; let refCount = 0 ; const finalizeTokenAttr = token => { if ( token . attrs && Array . isArray ( token . attrs ) ) token . attrs = token . attrs . map ( ( [ name , value ] ) => [ name , value . toString ( ) ] ) ; if ( token . type === 'inline' ) { for ( const t of token . children ) finalizeTokenAttr ( t ) ; } } ; md . core . process = state => { const { normalizeLink } = md ; if ( refCount === 0 ) originalURLMap = new WeakMap ( ) ; try { md . normalizeLink = url => { const imprimitiveUrl = new String ( normalizeLink . call ( md , url ) ) ; originalURLMap . set ( imprimitiveUrl , url ) ; return imprimitiveUrl ; } ; refCount += 1 ; return process . call ( md . core , state ) ; } finally { refCount -= 1 ; md . normalizeLink = normalizeLink ; if ( refCount === 0 ) { for ( const token of state . tokens ) finalizeTokenAttr ( token ) ; } } } ; md . inline . ruler2 . push ( 'marpit_parse_image' , ( { tokens } ) => { for ( const token of tokens ) { if ( token . type === 'image' ) { const options = token . content . split ( / \s+ / ) . filter ( s => s . length > 0 ) ; const url = token . attrGet ( 'src' ) ; const originalUrl = originalURLMap . has ( url ) ? originalURLMap . get ( url ) : url ; token . meta = token . meta || { } ; token . meta . marpitImage = _objectSpread ( _objectSpread ( { } , token . meta . marpitImage || { } ) , { } , { url : url . toString ( ) , options } ) ; if ( ! ! _colorString . default . get ( originalUrl ) || originalUrl . toLowerCase ( ) === 'currentcolor' ) { token . meta . marpitImage . color = originalUrl ; token . hidden = true ; } for ( const opt of options ) { for ( const [ regexp , mergeFunc ] of optionMatchers ) { const matched = opt . match ( regexp ) ; if ( matched ) token . meta . marpitImage = _objectSpread ( _objectSpread ( { } , token . meta . marpitImage ) , mergeFunc ( matched , _objectSpread ( { filters : [ ] } , token . meta . marpitImage ) ) ) ; } } } } } ) ; }
Marpit image parse plugin.
Marpit image parse plugin .
// Store original URL, for the color shorthand. // (Avoid a side effect from link normalization) // Convert imprimitive attribute value into primitive string // Apply finalization recursively to inline tokens // Prevent reset of WeakMap caused by calling core process internally // Apply finalization for every tokens // Detect shorthand for setting color (Use value before normalization) // Parse keyword through matchers