mardown-it 有序列表ios序号溢出解决办法

javascript 复制代码
const md = new MarkdownIt({ html: true })

if (Tools.browserEnv().isIOS()) {
  // 先保存默认规则,避免覆盖掉 ul 的逻辑
  const defaultOrderedListOpen = md.renderer.rules.ordered_list_open || function (tokens, idx, options, env, self) {
    return self.renderToken(tokens, idx, options);
  };
  const defaultOrderedListClose = md.renderer.rules.ordered_list_close || function (tokens, idx, options, env, self) {
    return self.renderToken(tokens, idx, options);
  };
  const defaultListItemOpen = md.renderer.rules.list_item_open || function (tokens, idx, options, env, self) {
    return self.renderToken(tokens, idx, options);
  };
  const defaultListItemClose = md.renderer.rules.list_item_close || function (tokens, idx, options, env, self) {
    return self.renderToken(tokens, idx, options);
  };

  // 只改 <ol>
  md.renderer.rules.ordered_list_open = function (tokens, idx, options, env, self) {
    const token = tokens[idx];
    if (token.type !== 'ordered_list_open') {
      return defaultOrderedListOpen(tokens, idx, options, env, self);
    }
    return '<div class="markdown-list">\n';
  };

  md.renderer.rules.ordered_list_close = function (tokens, idx, options, env, self) {
    const token = tokens[idx];
    if (token.type !== 'ordered_list_close') {
      return defaultOrderedListClose(tokens, idx, options, env, self);
    }
    return '</div>\n';
  };

  // 只改 <ol> 下的 <li>
  md.renderer.rules.list_item_open = function (tokens, idx, options, env, self) {
    const token = tokens[idx];

    // 向上找父级
    let parent;
    for (let i = idx - 1; i >= 0; i--) {
      if (tokens[i].type.endsWith('_open') && tokens[i].level === token.level - 1) {
        parent = tokens[i];
        break;
      }
    }

    // 不是 ol → 默认渲染
    if (!parent || parent.type !== 'ordered_list_open') {
      return defaultListItemOpen(tokens, idx, options, env, self);
    }

    // --- 处理 ol ---
    let order = 1;
    if (parent.attrGet('start')) {
      order = parseInt(parent.attrGet('start'));
    }

    // 计算 li 的序号
    let liIndex = 0;
    for (let i = idx; i >= 0; i--) {
      if (tokens[i].type === 'list_item_open' && tokens[i].level === token.level) {
        liIndex++;
      } else if (tokens[i].type === 'ordered_list_open' && tokens[i].level < token.level) {
        break;
      }
    }

    return `<div class="markdown-list-item"><span class="number">${order + liIndex - 1}.</span><span class="text">`;
  };

  md.renderer.rules.list_item_close = function (tokens, idx, options, env, self) {
    const token = tokens[idx];

    // 向上找父级
    let parent;
    for (let i = idx - 1; i >= 0; i--) {
      if (tokens[i].type.endsWith('_open') && tokens[i].level === token.level - 1) {
        parent = tokens[i];
        break;
      }
    }

    // 不是 ol → 默认渲染
    if (!parent || parent.type !== 'ordered_list_open') {
      return defaultListItemClose(tokens, idx, options, env, self);
    }

    return '</span></div>\n';
  };
}

style:

css 复制代码
.markdown-list {
  padding-left: 0;
}

.markdown-list-item {
  display: flex;
  align-items: flex-start;
  // margin-bottom: 0.5em;
}

.markdown-list-item .number {
  // width: 3ch; /* 适应最大编号长度 */
  margin-right: 0.5em;
  text-align: left;
  // font-variant-numeric: tabular-nums;
  flex-shrink: 0;
}

.markdown-list-item .text {
  flex: 1;
}
相关推荐
Digitally4 小时前
如何将 iPhone 备份到电脑/PC 的前 5 种方法
ios·电脑·iphone
Swift社区5 小时前
在企业内部分发 iOS App 时如何生成并使用 manifest.plist
macos·ios·cocoa
Zz_waiting.5 小时前
Javaweb - 14.6 - Vue3 数据交互 Axios
开发语言·前端·javascript·vue·axios
GISer_Jing6 小时前
滴滴二面(准备二)
前端·javascript·vue·reactjs
F2E_Zhangmo7 小时前
基于cornerstone3D的dicom影像浏览器 第一章 webpack5+vue2+cornerstonejs项目创建
vue·cornerstone·cornerstone3d·cornerstonejs
他们都不看好你,偏偏你最不争气8 小时前
【iOS】push 和 present
ios
2501_9160137411 小时前
HTTPS 抓包难点分析,从端口到工具的实战应对
网络协议·http·ios·小程序·https·uni-app·iphone
2501_9159184114 小时前
uni-app 项目 iOS 上架效率优化 从工具选择到流程改进的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张14 小时前
如何在不同 iOS 设备上测试和上架 uni-app 应用 实战全流程解析
android·ios·小程序·https·uni-app·iphone·webview