Skip to content

HtmlConverter

A helper class to convert HTML to Markdown.

See Also:

  • https://github.com/thephpleague/html-to-markdown/ - Latest version on GitHub.

Properties

environment

protected \League\HTMLToMarkdown\Environment $environment

Methods

__construct

Constructor

public __construct(\League\HTMLToMarkdown\Environment|array<string,mixed> $options = []): mixed

Parameters:

Parameter Type Description
$options \League\HTMLToMarkdown\Environment|array Environment object or configuration options

getEnvironment

public getEnvironment(): \League\HTMLToMarkdown\Environment

getConfig

public getConfig(): \League\HTMLToMarkdown\Configuration

__invoke

Convert

public __invoke(string $html): string

Parameters:

Parameter Type Description
$html string

Return Value:

The Markdown version of the html

See Also:

  • \League\HTMLToMarkdown\HtmlConverter::convert -

convert

Convert

public convert(string $html): string

Loads HTML and passes to getMarkdown()

Parameters:

Parameter Type Description
$html string

Return Value:

The Markdown version of the html

Throws:


createDOMDocument

private createDOMDocument(string $html): \DOMDocument

Parameters:

Parameter Type Description
$html string

replaceMisplacedComments

Finds any comment nodes outside element and moves them into .

private replaceMisplacedComments(\DOMDocument $document): void

Parameters:

Parameter Type Description
$document \DOMDocument

See Also:

  • https://github.com/thephpleague/html-to-markdown/issues/212 - * https://3v4l.org/7bC33 -

convertChildren

Convert Children

private convertChildren(\League\HTMLToMarkdown\ElementInterface $element): void

Recursive function to drill into the DOM and convert each node into Markdown from the inside out.

Finds children of each node and convert those to #text nodes containing their Markdown equivalent, starting with the innermost element and working up to the outermost element.

Parameters:

Parameter Type Description
$element \League\HTMLToMarkdown\ElementInterface

convertToMarkdown

Convert to Markdown

protected convertToMarkdown(\League\HTMLToMarkdown\ElementInterface $element): string

Converts an individual node into a #text node containing a string of its Markdown equivalent.

Example: An

node with text content of 'Title' becomes a text node with content of '### Title'

Parameters:

Parameter Type Description
$element \League\HTMLToMarkdown\ElementInterface

Return Value:

The converted HTML as Markdown


sanitize

protected sanitize(string $markdown): string

Parameters:

Parameter Type Description
$markdown string

setOptions

Pass a series of key-value pairs in an array; these will be passed through the config and set.

public setOptions(array<string,mixed> $options): $this

The advantage of this is that it can allow for static use (IE in Laravel). An example being:

HtmlConverter::setOptions(['strip_tags' => true])->convert('

test

');

Parameters:

Parameter Type Description
$options array


Automatically generated on 2025-03-18