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;
}
相关推荐
Digitally3 分钟前
5种将照片从iPhone传输到戴尔PC/笔记本电脑的方法
ios·电脑·iphone
ajassi20009 分钟前
开源 Objective-C IOS 应用开发(三)第一个iPhone的APP
ios·开源·objective-c
Daniel_Coder6 小时前
iOS Widget 开发-9:可配置 Widget:使用 IntentConfiguration 实现参数选择
ios·swiftui·swift·widget·intents
合作小小程序员小小店8 小时前
web网页开发,在线%考试管理%系统,基于Idea,vscode,html,css,vue,java,maven,springboot,mysql
java·前端·系统架构·vue·intellij-idea·springboot
非专业程序员Ping9 小时前
Vibe Coding 实战!花了两天时间,让 AI 写了一个富文本渲染引擎!
ios·ai·swift·claude·vibecoding
00后程序员张11 小时前
HTTP抓包工具推荐,Fiddler配置方法、代理设置与使用教程详解(开发者必学网络调试技巧)
网络·http·ios·小程序·fiddler·uni-app·webview
.NET快速开发框架11 小时前
国思RDIF低代码快速开发框架 v6.2.2版本发布
低代码·vue·rdif·国思rdif开发框架
2501_9400940211 小时前
iphone Delta模拟器如何从夸克网盘导入游戏ROM 附游戏资源下载
游戏·ios·iphone
IT教程资源D12 小时前
[N_148]基于微信小程序网上书城系统
mysql·vue·uniapp·前后端分离·网上书城小程序·springboot书城
2501_9400940219 小时前
iPhone Delta模拟器游戏资源包合集中文游戏ROM+BIOS+Delta皮肤附游戏导入教程
游戏·ios·iphone