Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Updater to not remove useful README titles #770

Merged
merged 5 commits into from Mar 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/Packagist/WebBundle/Package/Updater.php
Expand Up @@ -629,12 +629,11 @@ private function prepareReadme($readme, $isGithub = false, $owner = null, $repo
}
}

// remove first title as it's usually the project name which we don't need
if ($dom->getElementsByTagName('h1')->length) {
$first = $dom->getElementsByTagName('h1')->item(0);
$first->parentNode->removeChild($first);
} elseif ($dom->getElementsByTagName('h2')->length) {
$first = $dom->getElementsByTagName('h2')->item(0);
// remove first page element if it's a <h1> or <h2>, because it's usually
// the project name or the `README` string which we don't need
$first = $dom->getElementsByTagName('body')->item(0)->childNodes->item(0);

if ($first && ('h1' === $first->nodeName || 'h2' === $first->nodeName)) {
$first->parentNode->removeChild($first);
}

Expand Down
28 changes: 28 additions & 0 deletions src/Packagist/WebBundle/Tests/Package/UpdaterTest.php
Expand Up @@ -104,6 +104,34 @@ public function testConvertsMarkdownForReadme()
self::assertSame($readmeHtml, $this->package->getReadme());
}

/**
* When <h1> or <h2> titles are not the first element of the README contents,
* they should not be removed.
*/
public function testNoUsefulTitlesAreRemovedForReadme()
{
$readme = <<<EOR
Lorem ipsum dolor sit amet.

# some title

EOR;
$readmeHtml = <<<EOR
<p>Lorem ipsum dolor sit amet.</p>
<h1>some title</h1>
EOR;

$this->driverMock->expects($this->any())->method('getRootIdentifier')->willReturn('master');
$this->driverMock->expects($this->any())->method('getComposerInformation')
->willReturn(['readme' => 'README.md']);
$this->driverMock->expects($this->once())->method('getFileContent')->with('README.md', 'master')
->willReturn($readme);

$this->updater->update($this->ioMock, $this->config, $this->package, $this->repositoryMock);

self::assertSame($readmeHtml, $this->package->getReadme());
}

public function testSurrondsTextReadme()
{
$this->driverMock->expects($this->any())->method('getRootIdentifier')->willReturn('master');
Expand Down