markdown-parity-check 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,7 @@ import { gfm } from 'micromark-extension-gfm';
4
4
  import { gfmFromMarkdown } from 'mdast-util-gfm';
5
5
  import { extractHtml } from './html.js';
6
6
  import { parseDocument } from 'htmlparser2';
7
- import { Element } from 'domhandler';
7
+ import { Element, Text } from 'domhandler';
8
8
  import { excerpt, extractNumbers, looseNormalize, normalizeCode, resolveHref, strictNormalize } from './normalize.js';
9
9
  /** Parses one inline HTML node (a single tag, per micromark) with htmlparser2. Returns null for comments,
10
10
  * processing instructions or anything that is not exactly one tag. */
@@ -229,14 +229,13 @@ function handle(ctx, node, listItem = false) {
229
229
  /** A raw HTML block in Markdown is parsed with the HTML extractor so its content takes part in the
230
230
  * comparison; the report shows that this happened. Comments and bare tags without text yield nothing. */
231
231
  function handleRawHtml(ctx, value, line) {
232
- const withoutComments = value.replace(/<!--[\s\S]*?-->/g, '');
233
- // Text check for the fallback warning: script, style, template and noscript are page machinery on both
234
- // sides, so their content does not count as skipped text.
235
- const visibleSource = withoutComments.replace(/<(script|style|template|noscript)\b[\s\S]*?<\/\1\s*>/gi, ' ');
236
- const textOnly = strictNormalize(visibleSource.replace(/<[^>]*>/g, ' '));
232
+ // Text check for the fallback warning, taken from the parsed DOM rather than from regex stripping:
233
+ // comments are not text, and script, style, template and noscript are page machinery on both sides,
234
+ // so their content does not count as skipped text.
235
+ const textOnly = visibleText(value);
237
236
  let parsed = null;
238
237
  try {
239
- parsed = extractHtml(`<body>${withoutComments}</body>`, { selector: 'body', baseUrl: ctx.base });
238
+ parsed = extractHtml(`<body>${value}</body>`, { selector: 'body', baseUrl: ctx.base });
240
239
  }
241
240
  catch {
242
241
  parsed = null;
@@ -256,6 +255,25 @@ function handleRawHtml(ctx, value, line) {
256
255
  ctx.issues.push({ code: 'MARKDOWN_RAW_HTML_SKIPPED', severity: 'warning', message: 'A raw HTML block in the Markdown carries text that could not be parsed into blocks; it was not compared.', line, excerpt: excerpt(textOnly) });
257
256
  }
258
257
  }
258
+ const MACHINERY = new Set(['script', 'style', 'template', 'noscript']);
259
+ /** Strictly normalized text of an HTML fragment as a parser sees it: text nodes only, comments and
260
+ * machinery elements excluded. */
261
+ function visibleText(fragment) {
262
+ const parts = [];
263
+ const walk = (nodes) => {
264
+ for (const node of nodes) {
265
+ if (node instanceof Text)
266
+ parts.push(node.data);
267
+ else if (node instanceof Element) {
268
+ if (MACHINERY.has(node.name.toLowerCase()))
269
+ continue;
270
+ walk(node.children);
271
+ }
272
+ }
273
+ };
274
+ walk(parseDocument(fragment).children);
275
+ return strictNormalize(parts.join(' '));
276
+ }
259
277
  const YAML_LINE = /^(?:[A-Za-z0-9_.-]+\s*:(?:\s|$)|\s+\S|-\s|#)/;
260
278
  const FENCE_RE = /^(?:\ufeff)?---[ \t]*\r?\n([\s\S]*?)\r?\n(?:---|\.\.\.)[ \t]*(?:\r?\n|$)/;
261
279
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "markdown-parity-check",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Compares the main content of a web page's HTML and Markdown representations and reports concrete differences.",
5
5
  "keywords": [
6
6
  "markdown",