Use front matter of the index page to define folder's name. Fixes #27

This commit is contained in:
Stéphane Goetz 2018-06-05 22:29:31 +02:00
parent eb89664473
commit 7b665558af
3 changed files with 43 additions and 3 deletions

View File

@ -75,10 +75,16 @@ class Builder
} }
if ($file->isDir()) { if ($file->isDir()) {
$new = new Directory($node, static::removeSortingInformations($file->getFilename()), $file); $title = static::removeSortingInformations($file->getFilename());
$new = new Directory($node, $title, $file);
$new->setName(static::getName($file->getPathName())); $new->setName(static::getName($file->getPathName()));
$new->setTitle(str_replace('_', ' ', static::removeSortingInformations($new->getName()))); $new->setTitle(str_replace('_', ' ', static::removeSortingInformations($new->getName())));
static::build($new, $ignore); static::build($new, $ignore);
$index = $new->getLocalIndexPage();
if ($index && $index->getTitle() != $title) {
$new->setTitle($index->getTitle());
}
} else { } else {
static::createContent($node, $file); static::createContent($node, $file);
} }

View File

@ -125,6 +125,16 @@ class Directory extends Entry implements \ArrayAccess, \IteratorAggregate
return $this->parent->getConfig(); return $this->parent->getConfig();
} }
public function getLocalIndexPage() {
$index_key = $this->getConfig()['index_key'];
if (isset($this->children[$index_key])) {
return $this->children[$index_key];
}
return false;
}
/** /**
* @return Content|null * @return Content|null
*/ */
@ -132,8 +142,8 @@ class Directory extends Entry implements \ArrayAccess, \IteratorAggregate
{ {
$index_key = $this->getConfig()['index_key']; $index_key = $this->getConfig()['index_key'];
if (isset($this->children[$index_key])) { if ($this->getLocalIndexPage()) {
return $this->children[$index_key]; return $this->getLocalIndexPage();
} }
if ($this->getConfig()->shouldInheritIndex() && $first_page = $this->seekFirstPage()) { if ($this->getConfig()->shouldInheritIndex() && $first_page = $this->seekFirstPage()) {

View File

@ -171,4 +171,28 @@ class BuilderTest extends TestCase
array_keys($tree->getEntries()) array_keys($tree->getEntries())
); );
} }
public function testIndexFrontMatter()
{
$structure = [
'folder' => [
'index.md' => "---\ntitle: new Title\n---\nThe content",
'Page.md' => 'another page',
'Button.md' => 'another page'
]
];
$root = vfsStream::setup('root', null, $structure);
$config = new Config;
$config->setDocumentationDirectory($root->url());
$config['valid_content_extensions'] = ['md'];
$config['mode'] = Daux::STATIC_MODE;
$config['index_key'] = 'index.html';
$tree = new Root($config);
Builder::build($tree, []);
$this->assertTrue(array_key_exists('folder', $tree->getEntries()));
$this->assertEquals('new Title', $tree->getEntries()['folder']->getTitle());
}
} }