-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cdc62ed
commit 4b47f65
Showing
8 changed files
with
392 additions
and
195 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace EditormdApp; | ||
|
||
use League\HTMLToMarkdown\Converter\ConverterInterface; | ||
use League\HTMLToMarkdown\ElementInterface; | ||
|
||
class HTMLToMarkdownCodeConverter implements ConverterInterface { | ||
/** | ||
* @param ElementInterface $element | ||
* | ||
* @return string | ||
*/ | ||
public function convert(ElementInterface $element) { | ||
$language = ''; | ||
|
||
// Checking for language class on the code block | ||
$classes = $element->getAttribute('class'); | ||
|
||
if ($classes) { | ||
// Since tags can have more than one class, we need to find the one that starts with 'language-' | ||
$classes = explode(' ', $classes); | ||
foreach ($classes as $class) { | ||
if (strpos($class, 'language-') !== false) { | ||
// Found one, save it as the selected language and stop looping over the classes. | ||
$language = str_replace('language-', '', $class); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
$markdown = ''; | ||
$code = html_entity_decode($element->getChildrenAsString()); | ||
|
||
// In order to remove the code tags we need to search for them and, in the case of the opening tag | ||
// use a regular expression to find the tag and the other attributes it might have | ||
$code = preg_replace('/<code\b[^>]*>/', '', $code); | ||
$code = str_replace('</code>', '', $code); | ||
|
||
// Checking if it's a code block or span | ||
if ($this->shouldBeBlock($element, $code)) { | ||
// Code block detected, newlines will be added in parent | ||
// $markdown .= '```' . $language . "\n" . $code . "\n" . '```'; | ||
$markdown .= '```' . $language . "\n" . $code . "" . '```'; | ||
} else { | ||
// One line of code, wrapping it on one backtick, removing new lines | ||
$markdown .= '`' . preg_replace('/\r\n|\r|\n/', '', $code) . '`'; | ||
} | ||
|
||
return $markdown; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getSupportedTags() { | ||
return array('code'); | ||
} | ||
|
||
/** | ||
* @param ElementInterface $element | ||
* @param string $code | ||
* | ||
* @return bool | ||
*/ | ||
private function shouldBeBlock(ElementInterface $element, $code) { | ||
if ($element->getParent()->getTagName() == 'pre') { | ||
return true; | ||
} | ||
|
||
if (preg_match('/[^\s]` `/', $code)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace EditormdApp; | ||
|
||
use League\HTMLToMarkdown\Converter\ConverterInterface; | ||
use League\HTMLToMarkdown\ElementInterface; | ||
|
||
class HTMLToMarkdownTableConverter implements ConverterInterface | ||
{ | ||
/** | ||
* @param ElementInterface $element | ||
* | ||
* @return string | ||
*/ | ||
public function convert(ElementInterface $element) | ||
{ | ||
switch ($element->getTagName()) { | ||
case 'tr': | ||
$line = []; | ||
$i = 1; | ||
foreach ($element->getChildren() as $td) { | ||
$i++; | ||
$v = $td->getValue(); | ||
$v = trim($v); | ||
if ($i % 2 === 0 || $v !== '') { | ||
$line[] = $v; | ||
} | ||
} | ||
return '| ' . implode(' | ', $line) . " |\n"; | ||
|
||
case 'td': | ||
case 'th': | ||
return trim($element->getValue()); | ||
|
||
case 'tbody': | ||
return trim($element->getValue()); | ||
|
||
case 'thead': | ||
$headerLine = reset($element->getChildren())->getValue(); | ||
$headers = explode(' | ', trim(trim($headerLine, "\n"), '|')); | ||
|
||
$hr = []; | ||
foreach ($headers as $td) { | ||
$length = strlen(trim($td)) + 2; | ||
$hr[] = str_repeat('-', $length > 3 ? $length : 3); | ||
} | ||
$hr = '|' . implode('|', $hr) . '|'; | ||
|
||
return $headerLine . $hr . "\n"; | ||
case 'table': | ||
$inner = $element->getValue(); | ||
if (strpos($inner, '-----') === false) { | ||
$inner = explode("\n", $inner); | ||
$single = explode(' | ', trim($inner[0], '|')); | ||
$hr = []; | ||
foreach ($single as $td) { | ||
$length = strlen(trim($td)) + 2; | ||
$hr[] = str_repeat('-', $length > 3 ? $length : 3); | ||
} | ||
$hr = '|' . implode('|', $hr) . '|'; | ||
array_splice($inner, 1, 0, $hr); | ||
$inner = implode("\n", $inner); | ||
} | ||
return trim($inner) . "\n\n"; | ||
} | ||
return $element->getValue(); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getSupportedTags() | ||
{ | ||
return array('table', 'tr', 'thead', 'td', 'tbody'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters