INNER CODE UNIT · TypeScript

fixZhihuListStructure

RyanYipeng/SyncCaster · apps/extension/src/content-scripts/index.ts:184

function fixZhihuListStructure(container: HTMLElement): void {
  // 知乎可能将嵌套列表放在 li 外面,需要修复
  container.querySelectorAll('ul, ol').forEach((list) => {
    const items = list.querySelectorAll(':scope > li');
    items.forEach((li) => {
      // 检查 li 后面是否紧跟着一个 ul/ol(应该在 li 内部)
      const nextSibling = li.nextElementSibling;
      if (nextSibling && (nextSibling.tagName === 'UL' || nextSibling.tagName === 'OL')) {
        // 检查这个嵌套列表是否应该属于当前 li
        // 如果嵌套列表的缩进更深,则移动到 li 内部
        const nestedList = nextSibling;
        // 将嵌套列表移动到 li 内部
        li.appendChild(nestedList);
        console.log('[zhihu] Fixed nested list structure');
      }
    });
  });
  

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…