Skip to content

Commit

Permalink
Add --unsafe flag, fixes #177
Browse files Browse the repository at this point in the history
  • Loading branch information
danburzo committed Jul 21, 2024
1 parent deaf0b1 commit 13f75e4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ const DEFAULT_MARKDOWN_OPTIONS = {
};
```

#### `--unsafe`

Disables some [JSDOM validations](https://github.com/jsdom/jsdom/blob/main/lib/jsdom/living/helpers/validate-names.js) that may throw an error when parsing invalid HTML pages (See [#177](https://github.com/danburzo/percollate/issues/177)).

## Recipes

### Basic bundling
Expand Down
3 changes: 3 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ Commmon options:
--inline Embed images inline with the content.
Fetches and converts images to Base64 'data:' URLs.
--unsafe Disable some validations in JSDOM to suppress some
errors thrown for invalid HTML inputs.
Options to disable features:
--no-amp Don't prefer the AMP version of the web page.
Expand Down
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import pup from 'puppeteer';
import archiver from 'archiver';
import fetch from 'node-fetch';
import { JSDOM } from 'jsdom';
import validateNames from 'jsdom/lib/jsdom/living/helpers/validate-names.js';

import nunjucks from 'nunjucks';
import css from 'css';
import { Readability } from '@mozilla/readability';
Expand Down Expand Up @@ -229,6 +231,18 @@ async function cleanup(url, options) {
? url
: 'file://' + path.resolve(url);

/*
Disable some validations in JSDOM to allow
some invalid HTML files to be processed correctly.
Currently done via monkey-patching. See:
https://github.com/danburzo/percollate/issues/177
*/
if (options.unsafe) {
// make no-op
validateNames.name = () => {};
}

const dom = new JSDOM(buffer, {
contentType,
url: final_url
Expand Down

0 comments on commit 13f75e4

Please sign in to comment.