-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(documentator):
Document::toSplittable()
will remove self links …
…(+ added `SelfLinksRemove` mutation).
- Loading branch information
1 parent
b76ee17
commit 7842200
Showing
16 changed files
with
222 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
packages/documentator/src/Markdown/Mutations/SelfLinksRemove.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Documentator\Markdown\Mutations; | ||
|
||
use LastDragon_ru\LaraASP\Documentator\Markdown\Contracts\Mutation; | ||
use LastDragon_ru\LaraASP\Documentator\Markdown\Data\Data; | ||
use LastDragon_ru\LaraASP\Documentator\Markdown\Data\Offset; | ||
use LastDragon_ru\LaraASP\Documentator\Markdown\Document; | ||
use LastDragon_ru\LaraASP\Documentator\Markdown\Utils; | ||
use League\CommonMark\Extension\CommonMark\Node\Inline\Link; | ||
use League\CommonMark\Node\Block\Document as DocumentNode; | ||
use Override; | ||
|
||
use function rawurldecode; | ||
|
||
/** | ||
* Removes all links to the self. | ||
*/ | ||
readonly class SelfLinksRemove implements Mutation { | ||
public function __construct() { | ||
// empty | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
#[Override] | ||
public function __invoke(Document $document, DocumentNode $node): iterable { | ||
// Just in case | ||
yield from []; | ||
|
||
// Update | ||
$links = $this->getLinks($document, $node); | ||
|
||
foreach ($links as $link) { | ||
// Location & Offset? | ||
$location = Utils::getLocation($link); | ||
$offset = Data::get($link, Offset::class); | ||
|
||
if (!$location || !$offset) { | ||
continue; | ||
} | ||
|
||
// Changes | ||
yield [Utils::getLengthLocation($location, 1), null]; // [ | ||
yield [Utils::getOffsetLocation($location, $offset - 1), null]; // ](...) | ||
} | ||
|
||
// Return | ||
return true; | ||
} | ||
|
||
/** | ||
* @return list<Link> | ||
*/ | ||
protected function getLinks(Document $document, DocumentNode $node): array { | ||
$links = []; | ||
|
||
foreach ($node->iterator() as $child) { | ||
if ($child instanceof Link && $this->isLink($document, rawurldecode($child->getUrl()))) { | ||
$links[] = $child; | ||
} | ||
} | ||
|
||
return $links; | ||
} | ||
|
||
protected function isLink(Document $document, string $url): bool { | ||
return Utils::isPathRelative($url) && Utils::isPathToSelf($url, $document); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
packages/documentator/src/Markdown/Mutations/SelfLinksRemoveTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Documentator\Markdown\Mutations; | ||
|
||
use LastDragon_ru\LaraASP\Documentator\Markdown\Document; | ||
use LastDragon_ru\LaraASP\Documentator\Markdown\Editor; | ||
use LastDragon_ru\LaraASP\Documentator\Testing\Package\TestCase; | ||
use League\CommonMark\Node\Block\Document as DocumentNode; | ||
use Override; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
|
||
use function array_key_first; | ||
use function array_values; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
#[CoversClass(SelfLinksRemove::class)] | ||
final class SelfLinksRemoveTest extends TestCase { | ||
public function testInvoke(): void { | ||
$markdown = <<<'MARKDOWN' | ||
# Header | ||
Text text [link](https://example.com) text text [`link`][link] text | ||
text text [self][self] text text [self](#fragment) text text text | ||
text text ![image][image] text text ![image](#fragment). | ||
[self]: #fragment | ||
[link]: ./path/to/file.md | ||
[image]: ./#fragment | ||
# Special | ||
## Inside Quote | ||
> Text text [self][self] text text [self](#fragment) text text text | ||
## Inside Table | ||
| Header | [Header][link] | | ||
|-------------------------|-------------------------------| | ||
| Cell [link][self] cell. | Cell `\|` \\| ![table][image] | | ||
| Cell | Cell cell [table][self]. | | ||
MARKDOWN; | ||
$document = new class($markdown, 'path/to/file.md') extends Document { | ||
#[Override] | ||
public function getNode(): DocumentNode { | ||
return parent::getNode(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
#[Override] | ||
public function getLines(): array { | ||
return parent::getLines(); | ||
} | ||
}; | ||
$node = $document->getNode(); | ||
$lines = $document->getLines(); | ||
$offset = (int) array_key_first($lines); | ||
$mutation = new SelfLinksRemove(); | ||
$changes = $mutation($document, $node); | ||
$actual = (string) (new Editor(array_values($lines), $offset))->mutate($changes); | ||
|
||
self::assertEquals( | ||
<<<'MARKDOWN' | ||
# Header | ||
Text text [link](https://example.com) text text [`link`][link] text | ||
text text self text text self text text text | ||
text text ![image][image] text text ![image](#fragment). | ||
[self]: #fragment | ||
[link]: ./path/to/file.md | ||
[image]: ./#fragment | ||
# Special | ||
## Inside Quote | ||
> Text text self text text self text text text | ||
## Inside Table | ||
| Header | [Header][link] | | ||
|-------------------------|-------------------------------| | ||
| Cell link cell. | Cell `\|` \\| ![table][image] | | ||
| Cell | Cell cell table. | | ||
MARKDOWN, | ||
$actual, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
.../Preprocess/Instructions/IncludeDocBlock/InstructionTest/ValidOnlyDescriptionExpected.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Description description description description description description | ||
description description description description description description | ||
description description description description description description | ||
[description](https://example.com/) description description. | ||
|
||
Description with inline tags: | ||
|
||
- `\B`, `\B`, `\B::b()` | ||
- `\A`, `\A`, `\A::a()` | ||
- `\LastDragon_ru\LaraASP\Documentator\Processor\Tasks\Preprocess\Instructions\IncludeDocBlock\Instruction` | ||
- `\LastDragon_ru\LaraASP\Documentator\Processor\Tasks\Preprocess\Instructions\IncludeDocBlock\Instruction::getName()` |
2 changes: 1 addition & 1 deletion
2
...asks/Preprocess/Instructions/IncludeDocBlock/InstructionTest/ValidOnlySummaryExpected.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
Summary [A](https://example.com/). | ||
Summary [A](https://example.com/) B C. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...process/Instructions/IncludeDocumentList/InstructionTest/nested/A/Document A.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# Nested A | ||
# Nested [A](#fragment) | ||
|
||
Summary [text](../../Document.md). |
3 changes: 2 additions & 1 deletion
3
...reprocess/Instructions/IncludeDocumentList/InstructionTest/nested/Document C.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
<!-- markdownlint-disable --> | ||
|
||
Summary [text](../Document.md) summary [link][link] and summary[^1]. | ||
Summary [text](../Document.md) summary [link][link] and summary[^1] and [self](#fragment) and [self][self]. | ||
|
||
[link]: ../Document.md (title) | ||
[self]: #fragment | ||
|
||
[^1]: Footnote |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
...ons/IncludePackageList/InstructionTest/packages/package custom readme/CUSTOM.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
# The Package with custom readme | ||
# The Package with custom [readme](#fragment) | ||
|
||
Text with [link](README.md) and [link][link] and footnote[^note]. | ||
Text with [link](README.md) and [link][link] and footnote[^note] and [self](#fragment) and [self][self]. | ||
|
||
## Section | ||
|
||
Text. | ||
|
||
[link]: README.md (title) | ||
[self]: #fragment | ||
|
||
[^note]: Footnote. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters