-
Notifications
You must be signed in to change notification settings - Fork 705
Expand file tree
/
Copy pathMarkdown.php
More file actions
62 lines (51 loc) · 2.06 KB
/
Copy pathMarkdown.php
File metadata and controls
62 lines (51 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
declare(strict_types=1);
namespace CraftCms\Cms\Gql\Directives;
use CraftCms\Cms\Gql\GqlEntityRegistry;
use CraftCms\Cms\Support\Facades\Markdown as MarkdownFacade;
use GraphQL\Language\DirectiveLocation;
use GraphQL\Type\Definition\Directive as GqlDirective;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
class Markdown extends Directive
{
public const DEFAULT_FLAVOR = null;
public const DEFAULT_INLINE_ONLY = false;
public static function create(): GqlDirective
{
$typeName = static::name();
return GqlEntityRegistry::getOrCreate($typeName, fn () => new self([
'name' => $typeName,
'locations' => [
DirectiveLocation::FIELD,
],
'args' => [
[
'name' => 'flavor',
'type' => Type::string(),
'defaultValue' => self::DEFAULT_FLAVOR,
'description' => 'The “flavor” of Markdown the input should be interpreted with. Accepts the same flavor names as `CraftCms\\Cms\\Support\\Facades\\Markdown::parse()`.',
],
[
'name' => 'inlineOnly',
'type' => Type::boolean(),
'defaultValue' => self::DEFAULT_INLINE_ONLY,
'description' => 'Whether to only parse inline elements, omitting any `<p>` tags.',
],
],
'description' => 'Parses the passed field value as Markdown.',
]));
}
public static function name(): string
{
return 'markdown';
}
public static function apply(mixed $source, mixed $value, array $arguments, ResolveInfo $resolveInfo): mixed
{
$inlineOnly = $arguments['inlineOnly'] ?? self::DEFAULT_INLINE_ONLY;
if ($inlineOnly) {
return MarkdownFacade::parseParagraph((string) $value, $arguments['flavor'] ?? self::DEFAULT_FLAVOR);
}
return MarkdownFacade::parse((string) $value, $arguments['flavor'] ?? self::DEFAULT_FLAVOR);
}
}