HTML Stripper

Low-level HTML cleanup utilities for paste pipelines, attribute stripping, and source-specific normalization.

Access:

app.getService('htmlStripper')

App code:

const htmlStripper = app.getService('htmlStripper');

Plugin:

const htmlStripper = this.app.getService('htmlStripper');

extractStartFragment#

Strips clipboard StartFragment wrapper spans and the marker comment from HTML that begins with .

Parameters:

  • html (string) — HTML string

Returns: string

htmlStripper.extractStartFragment(clipboardHtml);

removeDoctype#

Removes DOCTYPE declarations from an HTML string.

Parameters:

  • html (string) — HTML string

Returns: string

htmlStripper.removeDoctype(html);

stripComments#

Removes HTML comments including StartFragment and EndFragment markers.

Parameters:

  • html (string) — HTML string

Returns: string

htmlStripper.stripComments(html);

removeFragmentEmptyTags#

Cleans empty paragraphs and wrapper spans from StartFragment clipboard HTML without removing the marker.

Parameters:

  • html (string) — HTML string

Returns: string

htmlStripper.removeFragmentEmptyTags(html);

removeBlockTagsInside#

Finds elements matching tags and unwraps nested block tags inside them, inserting br elements before unwrapping.

Parameters:

  • html (string) — HTML string
  • tags (Array) — tags

Returns: string

htmlStripper.removeBlockTagsInside(html, ['li']);

removeAllAttributes#

Strips element attributes except tag-specific keep lists and config allowedAttributes, allowedClasses, and allowedStyles.

Parameters:

  • html (string) — HTML string
  • config (object) — configuration object

Returns: string

htmlStripper.removeAllAttributes(html, { allowedClasses: ['bold'] });

removeTags#

Removes opening and closing tags whose tag names appear in the selectors list.

Parameters:

  • html (string) — HTML string
  • selectors (any) — selectors

Returns: string

htmlStripper.removeTags(html, ['meta', 'link']);

enforceTagAllowlist#

Unwraps any element whose tag is not in the union of paste.inlineTags, paste.blockTags, and paste.formTags.

Parameters:

  • html (string) — HTML string

Returns: string

htmlStripper.enforceTagAllowlist(html);

setAllLinksTarget#

Sets the target attribute on all anchor elements with an href.

Parameters:

  • html (string) — HTML string
  • target (string) — scroll target selector or element

Returns: string

htmlStripper.setAllLinksTarget(html, '_blank');

removeScriptAndStyle#

Removes script and style elements and their contents from HTML.

Parameters:

  • html (string) — HTML string

Returns: string

htmlStripper.removeScriptAndStyle(html);

removeNbspOnlySpans#

Replaces spans that contain only   with a regular space text node.

Parameters:

  • html (string) — HTML string

Returns: string

htmlStripper.removeNbspOnlySpans(html);

unwrapPlainSpans#

Unwraps span elements that have no attributes, preserving their child content.

Parameters:

  • html (string) — HTML string

Returns: string

htmlStripper.unwrapPlainSpans(html);

removeEmptyPlainInline#

Removes empty inline tags (no attributes and no text content) defined in editor tags.inline.

Parameters:

  • html (string) — HTML string

Returns: string

htmlStripper.removeEmptyPlainInline(html);

unwrapAnchorBlocks#

Unwraps anchor elements that wrap block-level content such as paragraphs, lists, or tables.

Parameters:

  • html (string) — HTML string

Returns: string

htmlStripper.unwrapAnchorBlocks(html);

normalizeTodoItems#

Converts data-block todo-item list items to plain text with [x] or [ ] checkbox prefixes.

Parameters:

  • html (string) — HTML string

Returns: string

htmlStripper.normalizeTodoItems(html);

cleanPages#

Removes Apple Pages sN and pN CSS class attributes from pasted HTML.

Parameters:

  • html (string) — HTML string

Returns: string

htmlStripper.cleanPages(html);

cleanGDocs#

Normalizes Google Docs paste HTML by unwrapping heading spans and converting style spans to semantic b/i tags.

Parameters:

  • html (string) — HTML string

Returns: string

htmlStripper.cleanGDocs(html);

cleanMsWord#

Cleans Microsoft Word paste HTML by parsing list levels, unwrapping Mso spans, and removing Office-specific markup.

Parameters:

  • html (string) — HTML string

Returns: string

htmlStripper.cleanMsWord(html);

normalizeLists#

Fixes list paste artifacts by unwrapping Apple-converted-space, nesting sibling lists into li elements, and flattening li > p.

Parameters:

  • html (string) — HTML string

Returns: string

htmlStripper.normalizeLists(html);

convertPicturesToImages#

Replaces picture elements with a single img using the first absolute source URL, removing pictures with unresolved relative paths.

Parameters:

  • html (string) — HTML string

Returns: string

htmlStripper.convertPicturesToImages(html);