-
Notifications
You must be signed in to change notification settings - Fork 27
/
Markdown.php
189 lines (165 loc) · 4.56 KB
/
Markdown.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
namespace Illuminate\Mail;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\Table\TableExtension;
use League\CommonMark\MarkdownConverter;
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
class Markdown
{
/**
* The view factory implementation.
*
* @var \Illuminate\Contracts\View\Factory
*/
protected $view;
/**
* The current theme being used when generating emails.
*
* @var string
*/
protected $theme = 'default';
/**
* The registered component paths.
*
* @var array
*/
protected $componentPaths = [];
/**
* Create a new Markdown renderer instance.
*
* @param \Illuminate\Contracts\View\Factory $view
* @param array $options
* @return void
*/
public function __construct(ViewFactory $view, array $options = [])
{
$this->view = $view;
$this->theme = $options['theme'] ?? 'default';
$this->loadComponentsFrom($options['paths'] ?? []);
}
/**
* Render the Markdown template into HTML.
*
* @param string $view
* @param array $data
* @param \TijsVerkoyen\CssToInlineStyles\CssToInlineStyles|null $inliner
* @return \Illuminate\Support\HtmlString
*/
public function render($view, array $data = [], $inliner = null)
{
$this->view->flushFinderCache();
$contents = $this->view->replaceNamespace(
'mail', $this->htmlComponentPaths()
)->make($view, $data)->render();
if ($this->view->exists($customTheme = Str::start($this->theme, 'mail.'))) {
$theme = $customTheme;
} else {
$theme = str_contains($this->theme, '::')
? $this->theme
: 'mail::themes.'.$this->theme;
}
return new HtmlString(($inliner ?: new CssToInlineStyles)->convert(
$contents, $this->view->make($theme, $data)->render()
));
}
/**
* Render the Markdown template into text.
*
* @param string $view
* @param array $data
* @return \Illuminate\Support\HtmlString
*/
public function renderText($view, array $data = [])
{
$this->view->flushFinderCache();
$contents = $this->view->replaceNamespace(
'mail', $this->textComponentPaths()
)->make($view, $data)->render();
return new HtmlString(
html_entity_decode(preg_replace("/[\r\n]{2,}/", "\n\n", $contents), ENT_QUOTES, 'UTF-8')
);
}
/**
* Parse the given Markdown text into HTML.
*
* @param string $text
* @return \Illuminate\Support\HtmlString
*/
public static function parse($text)
{
$environment = new Environment([
'allow_unsafe_links' => false,
]);
$environment->addExtension(new CommonMarkCoreExtension);
$environment->addExtension(new TableExtension);
$converter = new MarkdownConverter($environment);
return new HtmlString($converter->convert($text)->getContent());
}
/**
* Get the HTML component paths.
*
* @return array
*/
public function htmlComponentPaths()
{
return array_map(function ($path) {
return $path.'/html';
}, $this->componentPaths());
}
/**
* Get the text component paths.
*
* @return array
*/
public function textComponentPaths()
{
return array_map(function ($path) {
return $path.'/text';
}, $this->componentPaths());
}
/**
* Get the component paths.
*
* @return array
*/
protected function componentPaths()
{
return array_unique(array_merge($this->componentPaths, [
__DIR__.'/resources/views',
]));
}
/**
* Register new mail component paths.
*
* @param array $paths
* @return void
*/
public function loadComponentsFrom(array $paths = [])
{
$this->componentPaths = $paths;
}
/**
* Set the default theme to be used.
*
* @param string $theme
* @return $this
*/
public function theme($theme)
{
$this->theme = $theme;
return $this;
}
/**
* Get the theme currently being used by the renderer.
*
* @return string
*/
public function getTheme()
{
return $this->theme;
}
}