0.0.1 • Published 4 years ago

@yunlong.syl/markdown-loader v0.0.1

Weekly downloads
1
License
MIT
Repository
-
Last release
4 years ago

yunlong.syl/markdown-loader

实现逻辑参照markdown-loader 通过 marked 处理成 html 字符串 再用通过html-loader 转换为 js

为什么做这个

因为marked 中有下面一段对 url encode 的逻辑,我就想在md里面的图片路径和网址路径写中文

function cleanUrl(sanitize, base, href) {
  if (sanitize) {
    try {
      var prot = decodeURIComponent(unescape(href))
        .replace(/[^\w:]/g, '')
        .toLowerCase();
    } catch (e) {
      return null;
    }
    if (prot.indexOf('javascript:') === 0 || prot.indexOf('vbscript:') === 0 || prot.indexOf('data:') === 0) {
      return null;
    }
  }
  if (base && !originIndependentUrl.test(href)) {
    href = resolveUrl(base, href);
  }
  try {
    href = encodeURI(href).replace(/%25/g, '%');
  } catch (e) {
    return null;
  }
  return href;
}

how to use

params

配置项类型必填默认值功能/备注
decodeImgSrcbooleanfalsefalse是否解码 img 标签中被转义的字符 src
decodeAHrefbooleanfalsefalse是否解码 a 标签中被转义的字符 href
webpack.config.js

moudle: {
    rules: {
        {
          test: /\.md$/,
          use: [
            'html-loader', 
            {
              loader: '@yunlong.syl/markdown-loader',
              options: {
                decodeImgSrc: true,
                decodeAHref: true,
              }
            }
          ]
        }
    }
}