Skip to content

Commit 7b8ab05

Browse files
authored
Merge pull request #762 from hydephp/add-normalizeNewlines-helper
Add normalizeNewlines helper to the Hyde facade and refactor related methods
2 parents 870e134 + 82f4a9d commit 7b8ab05

File tree

6 files changed

+28
-17
lines changed

6 files changed

+28
-17
lines changed

packages/framework/src/Foundation/Concerns/ImplementsStringHelpers.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,26 @@
1616
*/
1717
trait ImplementsStringHelpers
1818
{
19-
public function makeTitle(string $slug): string
19+
public function makeTitle(string $value): string
2020
{
2121
$alwaysLowercase = ['a', 'an', 'the', 'in', 'on', 'by', 'with', 'of', 'and', 'or', 'but'];
2222

2323
return ucfirst(str_ireplace(
2424
$alwaysLowercase,
2525
$alwaysLowercase,
26-
Str::headline($slug)
26+
Str::headline($value)
2727
));
2828
}
2929

30-
public function markdown(string $text, bool $stripIndentation = false): HtmlString
30+
public function normalizeNewlines(string $string): string
3131
{
32-
if ($stripIndentation) {
33-
$text = MarkdownService::stripIndentation($text);
32+
return str_replace(["\r\n"], "\n", $string);
33+
}
34+
35+
public function markdown(string $text, bool $normalizeIndentation = false): HtmlString
36+
{
37+
if ($normalizeIndentation) {
38+
$text = MarkdownService::normalizeIndentationLevel($text);
3439
}
3540

3641
return new HtmlString(Markdown::render($text));

packages/framework/src/Framework/Actions/CreatesNewPageSourceFile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,6 @@ protected function createFile(string $contents): void
143143
{
144144
$this->prepareOutputDirectory();
145145

146-
file_put_contents($this->outputPath, str_replace(["\r\n"], "\n", $contents));
146+
file_put_contents($this->outputPath, Hyde::normalizeNewlines($contents));
147147
}
148148
}

packages/framework/src/Framework/Services/MarkdownService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ protected function enableAllHtmlElements(): void
213213
/**
214214
* Normalize indentation for an un-compiled Markdown string.
215215
*/
216-
public static function stripIndentation(string $string): string
216+
public static function normalizeIndentationLevel(string $string): string
217217
{
218218
$string = str_replace("\t", ' ', $string);
219219
$string = str_replace("\r\n", "\n", $string);

packages/framework/src/Hyde.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* @method static string image(string $name, bool $preferQualifiedUrl = false)
4040
* @method static string url(string $path = '')
4141
* @method static string makeTitle(string $slug)
42+
* @method static string normalizeNewlines(string $string)
4243
* @method static HtmlString markdown(string $text, bool $stripIndentation = false)
4344
* @method static string currentPage()
4445
* @method static string getBasePath()

packages/framework/tests/Feature/HydeKernelTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ public function test_make_title_helper_returns_title_from_page_slug()
9494
$this->assertEquals('Foo Bar', Hyde::makeTitle('foo-bar'));
9595
}
9696

97+
public function test_normalize_newlines_replaces_carriage_returns_with_unis_endings()
98+
{
99+
$this->assertEquals("foo\nbar\nbaz", Hyde::normalizeNewlines("foo\nbar\r\nbaz"));
100+
}
101+
97102
public function test_markdown_helper_converts_markdown_to_html()
98103
{
99104
$this->assertEquals(new HtmlString("<p>foo</p>\n"), Hyde::markdown('foo'));

packages/framework/tests/Feature/MarkdownServiceTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,64 +225,64 @@ public function test_stripIndentation_method_with_unindented_markdown()
225225
$service = $this->makeService();
226226

227227
$markdown = "foo\nbar\nbaz";
228-
$this->assertSame($markdown, $service->stripIndentation($markdown));
228+
$this->assertSame($markdown, $service->normalizeIndentationLevel($markdown));
229229
}
230230

231231
public function test_stripIndentation_method_with_indented_markdown()
232232
{
233233
$service = $this->makeService();
234234

235235
$markdown = "foo\n bar\n baz";
236-
$this->assertSame("foo\nbar\nbaz", $service->stripIndentation($markdown));
236+
$this->assertSame("foo\nbar\nbaz", $service->normalizeIndentationLevel($markdown));
237237

238238
$markdown = " foo\n bar\n baz";
239-
$this->assertSame("foo\nbar\nbaz", $service->stripIndentation($markdown));
239+
$this->assertSame("foo\nbar\nbaz", $service->normalizeIndentationLevel($markdown));
240240

241241
$markdown = " foo\n bar\n baz";
242-
$this->assertSame("foo\nbar\nbaz", $service->stripIndentation($markdown));
242+
$this->assertSame("foo\nbar\nbaz", $service->normalizeIndentationLevel($markdown));
243243
}
244244

245245
public function test_stripIndentation_method_with_tab_indented_markdown()
246246
{
247247
$service = $this->makeService();
248248

249249
$markdown = "foo\n\tbar\n\tbaz";
250-
$this->assertSame("foo\nbar\nbaz", $service->stripIndentation($markdown));
250+
$this->assertSame("foo\nbar\nbaz", $service->normalizeIndentationLevel($markdown));
251251
}
252252

253253
public function test_stripIndentation_method_with_carriage_return_line_feed()
254254
{
255255
$service = $this->makeService();
256256

257257
$markdown = "foo\r\n bar\r\n baz";
258-
$this->assertSame("foo\nbar\nbaz", $service->stripIndentation($markdown));
258+
$this->assertSame("foo\nbar\nbaz", $service->normalizeIndentationLevel($markdown));
259259
}
260260

261261
public function test_stripIndentation_method_with_code_indentation()
262262
{
263263
$service = $this->makeService();
264264

265265
$markdown = "foo\n bar\n baz";
266-
$this->assertSame("foo\nbar\n baz", $service->stripIndentation($markdown));
266+
$this->assertSame("foo\nbar\n baz", $service->normalizeIndentationLevel($markdown));
267267
}
268268

269269
public function test_stripIndentation_method_with_empty_newlines()
270270
{
271271
$service = $this->makeService();
272272

273273
$markdown = "foo\n\n bar\n baz";
274-
$this->assertSame("foo\n\nbar\nbaz", $service->stripIndentation($markdown));
274+
$this->assertSame("foo\n\nbar\nbaz", $service->normalizeIndentationLevel($markdown));
275275

276276
$markdown = "foo\n \n bar\n baz";
277-
$this->assertSame("foo\n \nbar\nbaz", $service->stripIndentation($markdown));
277+
$this->assertSame("foo\n \nbar\nbaz", $service->normalizeIndentationLevel($markdown));
278278
}
279279

280280
public function test_stripIndentation_method_with_trailing_newline()
281281
{
282282
$service = $this->makeService();
283283

284284
$markdown = "foo\n bar\n baz\n";
285-
$this->assertSame("foo\nbar\nbaz\n", $service->stripIndentation($markdown));
285+
$this->assertSame("foo\nbar\nbaz\n", $service->normalizeIndentationLevel($markdown));
286286
}
287287

288288
protected function makeService()

0 commit comments

Comments
 (0)