import { marked } from '../../../../../../utils/lib-marked';
import Log from '../../../../../../utils/logger';

const rendererMD = new marked.Renderer();
rendererMD.image = (href, title, text) => {
  return `
    <img src="${href}" alt="${text}" style="max-width: 100%;">
  `;
}
rendererMD.link = (href, title, text) => {
  if (href) {
    // 匹配以 http:// 或 https:// 开头，所有 URL 主体字符，遇到第一个非主体字符（如中文括号、空格、表情符号等）时停止
    let ret = href.replace(/https?:\/\/[\w\-./?=&:#]+(?=[^\w\-./?=&:#]|$)/g, (matchedUrl) => {
      let isURLInText = false;
      if (matchedUrl !== href) {
        // 如果 text 里包含 url，我们就用 matchedUrl 作为 a 标签的值；否则用 text 作为值
        isURLInText = true;
      }
      return `<a target="_blank" rel="noreferrer noopenner" class="message-marked_link" href="${matchedUrl || ''}" title="${title}">${isURLInText ? matchedUrl : text}</a>`;
    });
    if (ret === href) {
      Log.w(`Unable to extract url, href:${href}`);
    }
    return ret;
  }
  return text;
}

marked.setOptions({
  gfm: true,
  breaks: true,
  pedantic: false,
  sanitize: false,
  smartLists: true,
  smartypants: false,
});

// uni-app 的 jscore 版本比较低，不支持 Unicode 属性
// marked 库v4.0起使用了 /\p{L}\p{N}/gu 等 Unicode 属性正则表达式，这些特性在 ES2018 引入，低版本 jscore 不支持，会报错
// 使用传统的正则代替，以规避问题

// uni-app android 不支持 marked v7.0.5 或更高版本（会白屏，uni-app ios 可以用，为了更好的兼容性，目前都用 marked v4.0）
export const parseMarkdown = (text: string) => {
  return marked.parse(text, { renderer: rendererMD });
};
