Skip to content

Commit

Permalink
Add processors bundle configuration
Browse files Browse the repository at this point in the history
Allowing to tweaks the defaults and/or disable some of the built-in
processors
  • Loading branch information
ogizanagi committed Feb 22, 2023
1 parent d1c7b7f commit 039ceea
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 5 deletions.
82 changes: 82 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public function getConfigTreeBuilder(): TreeBuilder

$this->addProvidersSection($rootNode);
$this->addResolveLinksSection($rootNode);
$this->addProcessorsSection($rootNode);

return $treeBuilder;
}
Expand Down Expand Up @@ -224,6 +225,87 @@ private function addProvidersSection(ArrayNodeDefinition $rootNode): void
;
}

private function addProcessorsSection(ArrayNodeDefinition $rootNode): void
{
$rootNode
->fixXmlConfig('processor')
->children()
->arrayNode('processors')
->children()
->scalarNode('content_property')
->info('Key used by default processors to access and modify the main text of your contents')
->defaultValue('content')
->cannotBeEmpty()
->end()

->arrayNode('assets')
->info('Attempt to resolve local assets URLs using the Asset component for images and links. See AssetSProcessor.')
->canBeDisabled()
->end()

->arrayNode('external_links')
->canBeDisabled()
->children()
->booleanNode('open_in_new_tab')
->info('Automatically add target="_blank" to external links. See HtmlExternalLinksProcessor.')
->defaultValue(true)
->end()
->end()
->end()

->arrayNode('anchors')
->canBeDisabled()
->children()
->scalarNode('open_in_new_tab')
->info('Automatically add anchor links to elements with an id. See HtmlAnchorProcessor.')
->defaultValue('h1, h2, h3, h4, h5')
->end()
->end()
->end()

->arrayNode('html_title')
->canBeDisabled()
->info('Extract a content title from a HTML property by using the first available h1 tag. See ExtractTitleFromHtmlContentProcessor.')
->children()
->scalarNode('property')
->info('Property where to inject the title in your model')
->defaultValue('title')
->end()
->end()
->end()

->arrayNode('html_elements_ids')
->canBeDisabled()
->info('Add ids to titles, images and other HTML elements in the content. See HtmlIdProcessor.')
->end()

->arrayNode('code_highlight')
->canBeDisabled()
->info('Enabled the syntax highlighting for code blocks using Prism.js. See CodeHighlightProcessor.')
->end()

->arrayNode('toc')
->canBeDisabled()
->info('Build a table of content from the HTML titles. See TableOfContentProcessor.')
->children()
->scalarNode('property')
->info('Property used to configure and inject the TableOfContent object in your model')
->defaultValue('tableOfContent')
->end()
->integerNode('min_depth')
->defaultValue(2)
->end()
->integerNode('max_depth')
->defaultValue(6)
->end()
->end()
->end()
->end()
->end()
->end()
;
}

/**
* Filters out provider config entries by factory type.
*/
Expand Down
13 changes: 9 additions & 4 deletions src/Processor/HtmlAnchorProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ class HtmlAnchorProcessor implements ProcessorInterface
{
private HtmlCrawlerManagerInterface $crawlers;
private string $property;
private string $selector;

public function __construct(HtmlCrawlerManagerInterface $crawlers, string $property = 'content')
{
public function __construct(
HtmlCrawlerManagerInterface $crawlers,
string $property = 'content',
string $selector = 'h1, h2, h3, h4, h5'
) {
$this->crawlers = $crawlers;
$this->property = $property;
$this->selector = $selector;
}

public function __invoke(array &$data, Content $content): void
Expand All @@ -38,7 +43,7 @@ public function __invoke(array &$data, Content $content): void
return;
}

foreach ($crawler->filter('h1, h2, h3, h4, h5') as $element) {
foreach ($crawler->filter($this->selector) as $element) {
$this->addAnchor($element);
}

Expand All @@ -56,7 +61,7 @@ private function addAnchor(\DOMElement $element): void
return;
}

$child->appendXML('<a href="#' . $element->getAttribute('id') . '" class="anchor"></a>');
$child->appendXML(sprintf('<a href="#%s" class="anchor"></a>', $id));

$element->appendChild($child);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Processor/HtmlIdProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Symfony\Component\String\Slugger\SluggerInterface;

/**
* Add ids to title in the content
* Add ids to HTML elements in the content
*/
class HtmlIdProcessor implements ProcessorInterface
{
Expand Down

0 comments on commit 039ceea

Please sign in to comment.