Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions harness/preflight.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
}
Expand Down
66 changes: 66 additions & 0 deletions harness/test-css-declaration-count.mjs
Original file line number Diff line number Diff line change
@@ -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 =
'<li style="margin:0;color:#4d4f55;">Schultz KAP, et al. DICER1-related tumor predisposition: identification of at-risk individuals. <em>Clin Cancer Res</em> 30, 5681-5692 (2024).</li>';

const cases = [
["style 内 position:absolute", '<div style="position:absolute;top:0">x</div>', "position", 1],
["style 内冒号后带空格", '<section style="position: absolute">x</section>', "position", 1],
["分号之后的 position:fixed", '<p style="color:red;position:fixed">x</p>', "position", 1],
["冒号前带空格的 position :relative", '<p style="color:red; position :relative">x</p>', "position", 1],
["单引号属性值", "<div style='position:sticky'>x</div>", "position", 1],
["同一段 HTML 两处违规", '<div style="position:absolute"></div><span style="position:relative"></span>', "position", 2],
["参考文献标题 predisposition: 不是样式", REF_WITH_PREDISPOSITION, "position", 0],
["正文里的 disposition:", "<p>tumor disposition: a review</p>", "position", 0],
["标签属性值里的 predisposition:", '<li title="tumor predisposition: identification">x</li>', "position", 0],
["没有任何样式", "<p>plain text</p>", "position", 0],
["filter 真实违规", '<div style="filter:blur(2px)">x</div>', "filter", 1],
["filter 正文英文", "<p>high-pass filter: design</p>", "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(`<ol>${REF_WITH_PREDISPOSITION}</ol>`);
assert.equal(cleanDraft.positionCount, 0);
assert.equal(cleanDraft.filterCount, 0);
const dirtyDraft = analyzeDraftContent(`<div style="position:absolute">${REF_WITH_PREDISPOSITION}</div>`);
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, `<html><body>${body}</body></html>`, "utf8");
return runPreflight({ htmlPath, mdPath, allowLocalImagePaths: true, skipImageCheck: true });
};

const clean = await run(`<ol>${REF_WITH_PREDISPOSITION}</ol>`);
assert.equal(clean.l1.failures.some((item) => item.id === "html_compliance"), false);

const dirty = await run('<p style="position:absolute">x</p>');
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.");
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 17 additions & 3 deletions scripts/create_wechat_draft.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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(/<img/g) || []).length,
cdnCount: (content.match(/mmbiz\.qpic\.cn/g) || []).length,
Expand All @@ -314,8 +328,8 @@ function analyzeDraftContent(content) {
lightCardCount: (content.match(/background:#fff8f1/g) || []).length,
quoteCount: (content.match(/padding:14px 16px;border-left:3px/g) || []).length,
styleCount: (content.match(/style="/g) || []).length,
positionCount: (content.match(/position:[^\;"]+/g) || []).length,
filterCount: (content.match(/filter:[^\;"]+/g) || []).length,
positionCount: countCssDeclarations(content, "position"),
filterCount: countCssDeclarations(content, "filter"),
};
}

Expand Down
Loading