Skip to content

Commit ab5aeb4

Browse files
committed
Fixing indented yaml
BC break: Markdown blocks don't trim starting whitespace anymore.
1 parent 1508b82 commit ab5aeb4

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

spec/Mamazu/DocumentationParser/Validator/Yaml/YamlValidatorSpec.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,19 @@ public function it_adds_an_error_if_the_whole_document_is_invalid(
6767
$result->shouldHaveCount(1);
6868
$result[0]->shouldBeLike(new Error('test.md', 10, 'Invalid YAML', 'yaml'));
6969
}
70+
71+
public function it_validates_indented_block(Parser $parser, Block $block): void
72+
{
73+
$block->getContent()->willReturn(<<<YAML
74+
fixtures:
75+
testing: 1234
76+
YAML);
77+
78+
$parser->parse(<<<YAML
79+
fixtures:
80+
testing: 1234
81+
YAML);
82+
83+
$this->validate($block)->shouldHaveCount(0);
84+
}
7085
}

src/Parser/Parser/MarkdownParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function parse(string $fileName): array
3131
$type = substr($lineContent, 3);
3232
$beginLine = $lineNumber;
3333
} else {
34-
$blocks[] = new Block($fileName, trim($content), $beginLine + 1, $type);
34+
$blocks[] = new Block($fileName, rtrim($content), $beginLine + 1, $type);
3535
$beginLine = null;
3636
}
3737
} else {

src/Validator/Yaml/YamlValidator.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ public function __construct(?Parser $parser = null)
2121

2222
public function validate(Block $block): array
2323
{
24+
$cleanedContent = $this->cleanContent($block->getContent());
2425
try {
25-
$this->parser->parse($block->getContent());
26+
$this->parser->parse($cleanedContent);
2627
} catch (ParseException $exception) {
2728
// If the entire document is invalid $exception->getParsedLine() will be -1 so we just set it to 0
2829
$line = max(0, $exception->getParsedLine());
@@ -31,4 +32,32 @@ public function validate(Block $block): array
3132

3233
return [];
3334
}
35+
36+
private function cleanContent(string $content): string
37+
{
38+
$lineContent = explode("\n", $content);
39+
$offset = 0;
40+
foreach ($lineContent as $line) {
41+
if ($line === '') {
42+
continue;
43+
}
44+
45+
while ($offset < strlen($line)) {
46+
$char = $line[$offset];
47+
if (! ctype_space($char)) {
48+
break;
49+
}
50+
$offset++;
51+
}
52+
break;
53+
}
54+
55+
return implode(
56+
"\n",
57+
array_map(
58+
static fn (string $line): string => substr($line, $offset),
59+
$lineContent
60+
)
61+
);
62+
}
3463
}

0 commit comments

Comments
 (0)