Skip to content

Commit 5d0ed40

Browse files
committed
Add ability to configure snippet directories
1 parent c3452e4 commit 5d0ed40

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

CHANGELOG.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22

33
## Unreleased
44

5+
## 0.5.4 - 2021-09-06
6+
7+
### Added
8+
- Added the ability to configure the directories where Torchlight looks for snippets.
9+
510
## 0.5.3 - 2021-08-14
611

712
### Changed
8-
913
- Post-processors don't run if Laravel is compiling views.
1014

1115
### Added
12-
1316
- You can set `tab_width` to `false` to output literal tabs into the rendered HTML.
1417

1518
### Fixed
16-
1719
- Livewire middleware won't be registered for V1 of Livewire, since it's not possible.
1820

1921
## 0.5.2 - 2021-08-02

config/torchlight.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424
// `false` to leave literal tabs in the HTML.
2525
'tab_width' => 4,
2626

27+
// If you pass a filename to the code component or in a markdown
28+
// block, Torchlight will look for code snippets in the
29+
// following directories.
30+
'snippet_directories' => [
31+
resource_path()
32+
],
33+
2734
// Global options to control blocks-level settings.
2835
// https://torchlight.dev/docs/options
2936
'options' => [

src/Blade/CodeComponent.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Illuminate\View\Component;
99
use Torchlight\Block;
10+
use Torchlight\Torchlight;
1011

1112
class CodeComponent extends Component
1213
{
@@ -38,12 +39,7 @@ public function __construct($language, $theme = null, $contents = null, $torchli
3839
public function capture($contents)
3940
{
4041
$contents = $contents ?: $this->contents;
41-
42-
if (is_file($contents)) {
43-
$contents = file_get_contents($contents);
44-
} elseif (is_callable('resource_path') && is_file(resource_path($contents))) {
45-
$contents = file_get_contents(resource_path($contents));
46-
}
42+
$contents = Torchlight::processPotentialFileContents($contents);
4743

4844
BladeManager::registerBlock($this->block->code($contents));
4945
}

src/Manager.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,24 @@ public function postProcessBlocks($blocks)
150150
}
151151
}
152152

153+
public function processPotentialFileContents($file)
154+
{
155+
$directories = $this->config('snippet_directories', []);
156+
157+
// Add a blank path to account for absolute paths.
158+
array_unshift($directories, '');
159+
160+
foreach ($directories as $directory) {
161+
$directory = Str::finish($directory, DIRECTORY_SEPARATOR);
162+
163+
if (is_file($directory . $file)) {
164+
return file_get_contents($directory . $file);
165+
}
166+
}
167+
168+
return $file;
169+
}
170+
153171
/**
154172
* Get an item out of the config using dot notation.
155173
*

tests/MiddlewareAndComponentTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public function it_sends_a_simple_request_with_style()
8787
);
8888
}
8989

90-
9190
/** @test */
9291
public function classes_get_merged()
9392
{

0 commit comments

Comments
 (0)