Skip to content

Instantly share code, notes, and snippets.

@jmolivas
Created February 16, 2019 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmolivas/8ed70fb41833cf3b3d1491f1c83fcfc7 to your computer and use it in GitHub Desktop.
Save jmolivas/8ed70fb41833cf3b3d1491f1c83fcfc7 to your computer and use it in GitHub Desktop.

Convert body fields from HTML to Markdown

  • Install Drupal Console

  • Install PHP dependency

composer require league/html-to-markdown
  • Copy content of convert-html-markdown.php to console/snippet/

  • Execute drupal console snippet command

ahoy drupal snippet --file=console/snippet/convert-html-markdown.php
<?php
use League\HTMLToMarkdown\HtmlConverter;
$nodeStorage = \Drupal::entityTypeManager()
->getStorage('node');
$nodes = $nodeStorage->loadMultiple();
$converter = new HtmlConverter();
foreach ($nodes as $node) {
if (!$node->hasField('body')) {
continue;
}
echo 'Updating node ' . $node->id() . ' - ' . $node->label() . PHP_EOL;
$markdown = $converter->convert($body);
$node->set('body', $markdown);
$node->body->format = 'full_html';
$node->save();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment