Skip to content

Commit 8158267

Browse files
committed
Merge branch '2.x' of https://github.com/craftcms/html-field into 3.x
2 parents 50a56e4 + b28e5d2 commit 8158267

File tree

3 files changed

+131
-3
lines changed

3 files changed

+131
-3
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"prefer-stable": true,
2121
"require": {
2222
"php": "^8.2",
23-
"craftcms/cms": "^5.0.0-beta.1"
23+
"craftcms/cms": "^5.0.0-beta.1",
24+
"league/html-to-markdown": "^5.1"
2425
},
2526
"require-dev": {
2627
"craftcms/ecs": "dev-main",

composer.lock

Lines changed: 91 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/HtmlFieldData.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace craft\htmlfield;
99

1010
use Craft;
11+
use League\HTMLToMarkdown\HtmlConverter;
1112
use Twig\Markup;
1213

1314
/**
@@ -58,4 +59,41 @@ public function getParsedContent(): string
5859
{
5960
return (string)$this;
6061
}
62+
63+
/**
64+
* Returns the content as Markdown.
65+
*
66+
* @param array $config HtmlConverter configuration
67+
* @return string
68+
* @since 3.2.0
69+
*/
70+
public function getMarkdown(array $config = []): string
71+
{
72+
$converter = new HtmlConverter([
73+
'header_style' => 'atx',
74+
'remove_nodes' => 'meta style script',
75+
...$config,
76+
]);
77+
return $converter->convert($this->getParsedContent());
78+
}
79+
80+
/**
81+
* Returns the content as Markdown.
82+
*
83+
* @return string
84+
* @since 3.2.0
85+
*/
86+
public function getPlainText(): string
87+
{
88+
$text = $this->getMarkdown([
89+
'strip_tags' => true,
90+
'strip_placeholder_links' => true,
91+
'hard_break' => true,
92+
]);
93+
94+
// remove heading chars
95+
$text = preg_replace('/^#* /m', '', $text);
96+
97+
return $text;
98+
}
6199
}

0 commit comments

Comments
 (0)