diff --git a/CHANGELOG.md b/CHANGELOG.md index f7e02a1..bab6537 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ - **Bundle/relay 契约**:封面图纳入 bundle,relay 目录日期由本地生成,远程推送命令正确传递标题、作者、封面和 `--crop-235-1` - **发布摩擦沉淀**:补充 footer QR 绝对路径、参考资料 bullet 空行、手动 render 图片路径和正式推送默认 relay 的 gotchas - **Preflight 误报收敛**:Markdown 卡片/表格计数跳过代码块和 inline code,source verification 只扫描可见文本数字 +- **position/filter 样式误报**:`harness/preflight.mjs` 与 `create_wechat_draft.mjs` 用裸字符串匹配统计 `position:`/`filter:`,正文里的英文单词子串会被数成 CSS(2026-11-25 生产现场复现:参考文献标题「tumor predisposition: identification…」令本地 preflight 阻断且 `--auto-fix` 无法修复,relay 回检写出 `position: 1 ❌`,实际样式数为 0);两处改为只在标签内部统计 CSS 声明(`countCssDeclarations`),新增 `test-css-declaration-count.mjs` 用同一组用例钉住两份实现 - **CI 覆盖**:`npm run check` 与 GitHub Lint workflow 覆盖 `harness/**/*.mjs` ### Changed diff --git a/harness/preflight.mjs b/harness/preflight.mjs index d5f9678..b842818 100644 --- a/harness/preflight.mjs +++ b/harness/preflight.mjs @@ -419,10 +419,25 @@ function checkImageSizes(html, htmlDir, coverPath, rules) { return { passed: true, level: "L1", id: "image_size", checked: imagePaths.length }; } +// 只在「标签内部」统计 CSS 声明,避免把正文/标题里的英文单词误判为样式。 +// 例如参考文献标题 "tumor predisposition: identification..." 中的 "position:" 子串, +// 以及 "high-pass filter:" 之类,都不是 CSS。真实违规形如 style="position:absolute"。 +// scripts/create_wechat_draft.mjs 有一份逐字相同的实现(推送后回检用,运行在 relay 上、 +// 不能 import 本文件);两份由 harness/test-css-declaration-count.mjs 守护,改动须同步。 +export function countCssDeclarations(html, property) { + let count = 0; + const tagRe = /<([a-zA-Z][^<>]*)>/g; + let m; + while ((m = tagRe.exec(html)) !== null) { + count += (m[1].match(new RegExp(`(?:^|[;"'\\s])${property}\\s*:`, "g")) || []).length; + } + return count; +} + function checkHtmlCompliance(html, rules) { const issues = []; - const positionCount = (html.match(/position:/g) || []).length; - const filterCount = (html.match(/filter:/g) || []).length; + const positionCount = countCssDeclarations(html, "position"); + const filterCount = countCssDeclarations(html, "filter"); if (positionCount > 0) { issues.push(`Found ${positionCount} 'position:' style(s). WeChat strips position CSS.`); } diff --git a/harness/test-css-declaration-count.mjs b/harness/test-css-declaration-count.mjs new file mode 100644 index 0000000..d50e49b --- /dev/null +++ b/harness/test-css-declaration-count.mjs @@ -0,0 +1,66 @@ +#!/usr/bin/env node +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { countCssDeclarations as countInPreflight, runPreflight } from "./preflight.mjs"; +import { + analyzeDraftContent, + countCssDeclarations as countInDraftVerifier, +} from "../scripts/create_wechat_draft.mjs"; + +// 取自真实误报(2026-11):参考文献英文标题里的 "predisposition:" 含子串 "position:", +// 旧的裸字符串匹配把它数成了 1 处 position 样式。 +const REF_WITH_PREDISPOSITION = + '
  • Schultz KAP, et al. DICER1-related tumor predisposition: identification of at-risk individuals. Clin Cancer Res 30, 5681-5692 (2024).
  • '; + +const cases = [ + ["style 内 position:absolute", '
    x
    ', "position", 1], + ["style 内冒号后带空格", '
    x
    ', "position", 1], + ["分号之后的 position:fixed", '

    x

    ', "position", 1], + ["冒号前带空格的 position :relative", '

    x

    ', "position", 1], + ["单引号属性值", "
    x
    ", "position", 1], + ["同一段 HTML 两处违规", '
    ', "position", 2], + ["参考文献标题 predisposition: 不是样式", REF_WITH_PREDISPOSITION, "position", 0], + ["正文里的 disposition:", "

    tumor disposition: a review

    ", "position", 0], + ["标签属性值里的 predisposition:", '
  • x
  • ', "position", 0], + ["没有任何样式", "

    plain text

    ", "position", 0], + ["filter 真实违规", '
    x
    ', "filter", 1], + ["filter 正文英文", "

    high-pass filter: design

    ", "filter", 0], +]; + +for (const [name, html, property, expected] of cases) { + assert.equal(countInPreflight(html, property), expected, `preflight: ${name}`); + assert.equal(countInDraftVerifier(html, property), expected, `create_wechat_draft: ${name}`); +} + +// 端到端:推送后回检(这个函数运行在 relay 上,结果写进 audit.log) +const cleanDraft = analyzeDraftContent(`
      ${REF_WITH_PREDISPOSITION}
    `); +assert.equal(cleanDraft.positionCount, 0); +assert.equal(cleanDraft.filterCount, 0); +const dirtyDraft = analyzeDraftContent(`
    ${REF_WITH_PREDISPOSITION}
    `); +assert.equal(dirtyDraft.positionCount, 1); + +// 端到端:本地 preflight(推送前阻断) +const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), "md2wechat-css-count-")); +try { + const mdPath = path.join(tmpRoot, "article.md"); + fs.writeFileSync(mdPath, "---\nno_image: true\n---\n\n扫码加入讨论。", "utf8"); + let seq = 0; + const run = async (body) => { + const htmlPath = path.join(tmpRoot, `article-${seq++}.html`); + fs.writeFileSync(htmlPath, `${body}`, "utf8"); + return runPreflight({ htmlPath, mdPath, allowLocalImagePaths: true, skipImageCheck: true }); + }; + + const clean = await run(`
      ${REF_WITH_PREDISPOSITION}
    `); + assert.equal(clean.l1.failures.some((item) => item.id === "html_compliance"), false); + + const dirty = await run('

    x

    '); + assert.equal(dirty.ok, false); + assert.equal(dirty.l1.failures.some((item) => item.id === "html_compliance"), true); +} finally { + fs.rmSync(tmpRoot, { recursive: true, force: true }); +} + +console.log("CSS declaration count tests passed."); diff --git a/package.json b/package.json index 0c6afce..1cd11b6 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "render": "node scripts/render_wechat_editorial.mjs", "publish": "node scripts/create_wechat_draft.mjs", "relay:check": "node scripts/sync_relay_scripts.mjs --check --json", - "check": "find scripts harness -name '*.mjs' -print | sort | xargs -n1 node --check && node harness/test-code-generator-contract.mjs && node harness/test-preflight-fixtures.mjs && node harness/test-orchestrator-command-contract.mjs && node harness/test-relay-script-sync-contract.mjs && node harness/test-publish-evidence-contract.mjs && node harness/test-draft-update-shape.mjs && node harness/test-reconcile-wechat-drafts.mjs && node harness/test-self-report-no-write.mjs && node harness/run-generated-check-tests.mjs" + "check": "find scripts harness -name '*.mjs' -print | sort | xargs -n1 node --check && node harness/test-code-generator-contract.mjs && node harness/test-preflight-fixtures.mjs && node harness/test-css-declaration-count.mjs && node harness/test-orchestrator-command-contract.mjs && node harness/test-relay-script-sync-contract.mjs && node harness/test-publish-evidence-contract.mjs && node harness/test-draft-update-shape.mjs && node harness/test-reconcile-wechat-drafts.mjs && node harness/test-self-report-no-write.mjs && node harness/run-generated-check-tests.mjs" }, "engines": { "node": ">=18.0.0" diff --git a/scripts/create_wechat_draft.mjs b/scripts/create_wechat_draft.mjs index a9c9e0c..90d3ca3 100755 --- a/scripts/create_wechat_draft.mjs +++ b/scripts/create_wechat_draft.mjs @@ -304,7 +304,21 @@ function fetchDraftRecord(accessToken, mediaId) { return record; } -function analyzeDraftContent(content) { +// 只统计「标签内部」的 CSS 声明。微信返回的正文里若含英文单词子串(如参考文献标题 +// "tumor predisposition: identification…" 里的 "position:"),不应被当成样式。 +// harness/preflight.mjs 有一份逐字相同的实现(本地推送前检查);两份由 +// harness/test-css-declaration-count.mjs 守护,改动须同步。 +export function countCssDeclarations(content, property) { + let count = 0; + const tagRe = /<([a-zA-Z][^<>]*)>/g; + let m; + while ((m = tagRe.exec(content)) !== null) { + count += (m[1].match(new RegExp(`(?:^|[;"'\\s])${property}\\s*:`, "g")) || []).length; + } + return count; +} + +export function analyzeDraftContent(content) { return { imgCount: (content.match(/