Skip to content

Commit

Permalink
🐛 bugfix solves issue #129
Browse files Browse the repository at this point in the history
  • Loading branch information
carbontwelve committed Jul 6, 2017
1 parent e192c76 commit 6b0ed6a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
14 changes: 13 additions & 1 deletion src/Entities/Permalink.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,16 @@ public function getCompiled(File $file, $pretty = true)
$output = $this->template;
$output = str_replace('{ext}', $file->getExt(), $output);
$output = str_replace('{filename}', $this->sluggify($file->getFilename()), $output);
$output = str_replace('{path}', $file->getPath(), $output);

$filePath = str_replace('\\','/', $file->getPath());
if (substr($filePath, 0, 1) === '/'){
$filePath = substr($filePath, 1);
}
if (substr($filePath, -1, 1) === '/'){
$filePath = substr($filePath, 0, -1);
}
$filePath = preg_replace('!/+!', '/', $filePath);
$output = str_replace('{path}', $filePath, $output);

/** @var \DateTime $date */
if ($date = $file->getData('date')) {
Expand Down Expand Up @@ -93,6 +102,9 @@ public function getCompiled(File $file, $pretty = true)
$output = '/'.$output;
}

// Ensure valid slashes for url
$output = str_replace('\\','/', $output);

if ($pretty === true && $file->getData('pretty_permalink', true)) {
return $this->prettify($output);
}
Expand Down
40 changes: 35 additions & 5 deletions tests/PermalinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,25 @@
class PermalinkTest extends CommandTestBase
{
/**
* @param $filePath
* @param File $file
* @return Permalink
*/
private function setupPermalinks($filePath)
private function setupPermalinks(File $file)
{
return $file->getCompiledPermalink();
}

/**
* @param string $filePath
* @return File
*/
private function setupFile($filePath)
{
$file = new File(new SplFileInfo($filePath, '', ''));
$frontMatter = new FrontMatter($file->getFileContent());
$file->setData($frontMatter->getData());
$file->setContent($frontMatter->getContent());
return $file->getCompiledPermalink();
return $file;
}

/**
Expand All @@ -34,7 +43,7 @@ private function setupPermalinks($filePath)
public function testCategoryPermalinkTag()
{
// Synthetic Test
$this->assertEquals('/category1/category2/category3/test-md-post/index.html', $this->setupPermalinks(__DIR__ . '/mocks/TestCategoryPermalinkTag.md'));
$this->assertEquals('/category1/category2/category3/test-md-post/index.html', $this->setupPermalinks($this->setupFile(__DIR__ . '/mocks/TestCategoryPermalinkTag.md')));

// Full Test
$this->copyDirectory('assets/build_test_33/src', '_tmp');
Expand All @@ -45,6 +54,27 @@ public function testCategoryPermalinkTag()

public function testPrettyPermalink()
{
$this->assertEquals('/testfile/index.md', $this->setupPermalinks(__DIR__ . '/mocks/TestFile.md'));
$this->assertEquals('/testfile/index.md', $this->setupPermalinks($this->setupFile(__DIR__ . '/mocks/TestFile.md')));
}

public function testPermalinkPathSlashes()
{
$file = $this->setupFile(__DIR__ . '/mocks/TestFile.md');

$backSlashTest = $file;
$backSlashTest->setPath('hello\\world/123');
$this->assertEquals('/hello/world/123/testfile/index.md', $this->setupPermalinks($backSlashTest));

$beginningSlashTest = $file;
$beginningSlashTest->setPath('/hello/world/123');
$this->assertEquals('/hello/world/123/testfile/index.md', $this->setupPermalinks($beginningSlashTest));

$endingSlashTest = $file;
$endingSlashTest->setPath('/hello/world/123/');
$this->assertEquals('/hello/world/123/testfile/index.md', $this->setupPermalinks($endingSlashTest));

$doubleSlashTest = $file;
$doubleSlashTest->setPath('hello//world\\123/');
$this->assertEquals('/hello/world/123/testfile/index.md', $this->setupPermalinks($doubleSlashTest));
}
}

0 comments on commit 6b0ed6a

Please sign in to comment.