Skip to content

Commit 45741fe

Browse files
committed
Load files from markdown
1 parent da008ee commit 45741fe

5 files changed

Lines changed: 88 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Added
6+
- You can now load files from markdown by using the `<<< file.php` convention.
7+
38
## 0.4.1 - 2021-08-02
49

510
### Changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"require": {
2121
"php": "^7.2|^8.0",
2222
"league/commonmark": "^1.5",
23-
"torchlight/torchlight-laravel": "^0.5.0"
23+
"torchlight/torchlight-laravel": "^0.5.5"
2424
},
2525
"require-dev": {
2626
"orchestra/testbench": "^5.0|^6.0",

src/TorchlightExtension.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,32 @@ protected function makeTorchlightBlock($node)
9393
return Block::make()
9494
->language($this->getLanguage($node))
9595
->theme($this->getTheme($node))
96-
->code($node->getStringContent());
96+
->code($this->getContent($node));
9797
}
9898

9999
protected function isCodeNode($node)
100100
{
101101
return $node instanceof FencedCode || $node instanceof IndentedCode;
102102
}
103103

104+
protected function getContent($node)
105+
{
106+
$content = $node->getStringContent();
107+
108+
if (!Str::startsWith($content, '<<<')) {
109+
return $content;
110+
}
111+
112+
$file = trim(Str::after($content, '<<<'));
113+
114+
// It must be only one line, because otherwise it might be a heredoc.
115+
if (count(explode("\n", $file)) > 1) {
116+
return $content;
117+
}
118+
119+
return Torchlight::processFileContents($file) ?: $content;
120+
}
121+
104122
protected function getInfo($node)
105123
{
106124
if (!$this->isCodeNode($node) || $node instanceof IndentedCode) {

tests/CodeRendererTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,68 @@ public function can_set_theme()
166166
});
167167
}
168168

169+
/** @test */
170+
public function can_load_file()
171+
{
172+
config()->set('torchlight.snippet_directories', [
173+
__DIR__
174+
]);
175+
176+
$markdown = <<<'EOT'
177+
```
178+
<<< Support/file1.php
179+
```
180+
EOT;
181+
182+
Http::fake();
183+
184+
$this->render($markdown);
185+
186+
Http::assertSent(function ($request) {
187+
return $request['blocks'][0]['code'] === '// this is file 1';
188+
});
189+
}
190+
191+
/** @test */
192+
public function non_existent_file_just_stays()
193+
{
194+
$markdown = <<<'EOT'
195+
```
196+
<<< Support/nonexistent.php
197+
```
198+
EOT;
199+
200+
Http::fake();
201+
202+
$this->render($markdown);
203+
204+
Http::assertSent(function ($request) {
205+
return $request['blocks'][0]['code'] === '<<< Support/nonexistent.php';
206+
});
207+
}
208+
209+
210+
/** @test */
211+
public function doesnt_load_heredoc()
212+
{
213+
$markdown = <<<'EOT'
214+
```
215+
<<<SQL
216+
select 1;
217+
SQL;
218+
```
219+
EOT;
220+
221+
Http::fake();
222+
223+
$this->render($markdown);
224+
225+
Http::assertSent(function ($request) {
226+
return $request['blocks'][0]['code'] === "<<<SQL\nselect 1;\nSQL;";
227+
});
228+
}
229+
230+
169231
/** @test */
170232
public function it_sends_one_request_only_and_matches_by_id()
171233
{

tests/Support/file1.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// this is file 1

0 commit comments

Comments
 (0)