I started writing a plugin for the getkirby.com docs to automatically generate the JSON snippets.
Hereās what I have so far:
JSON output
"$field->escape()": {
"prefix": "->escape()",
"body": "escape(${1})",
"description": "Escapes the field value to be safely used in HTML templates without the risk of XSS attacks",
"scope": "php"
},
"$field->excerpt()": {
"prefix": "->excerpt()",
"body": "excerpt(${1})",
"description": "Creates an excerpt of the field value without html or any other formatting.",
"scope": "php"
},
...
Plugin code
<?php
Kirby::plugin('medienbaecker/snippet-generator', [
'routes' => [
[
'pattern' => 'docs/snippets.json',
'action' => function () {
if($reference = page("docs/reference")) {
$json = array();
if($fieldMethods = page("docs/reference/templates/field-methods")) {
foreach($fieldMethods->children()->visible() as $fieldMethod) {
$json[$fieldMethod->title()->value()] = array(
"prefix" => '->' . $fieldMethod->content()->get('title') . '()',
"body" => $fieldMethod->content()->get('title') . '(${1})',
"description" => $fieldMethod->excerpt()->value(),
"scope" => "php"
);
}
}
return $json;
}
}
]
]
]);
I must admit I donāt fully understand everything about the getkirby.com website yet, so right now I have no idea how to get the tab stops working. There seems to be a ->methodCall()
function but it outputs a lot more than I need/want:
Output of $fieldMethod->methodCall()
:
$field->excerpt(int $chars = 0, bool $strip = true, string $rep = 'ā¦'):
Needed for Visual Studio Code:
excerpt(${1:\\$chars = 0}, ${2:\\$strip = true}, ${3:\\$rep = '...'})
I could probably convert it with some Regex voodoo, but as I donāt even know where Kirby gets these parameters there might be an easier way. Iād appreciate any help!
Edit: I just found this code in site/plugins/site/src/Models/HelperPage.php. Canāt seem to find any way to use this ->parameters()
thingy, though.
Edit 2: Nevermind. I found out how I can output the parameters:
$parameters = array_column($fieldMethod->parameters(), 'export');
$body = array();
$c = 0;
foreach($parameters as $parameter) {
$c++;
$body[] = '${' . $c . ':\\' . explode(" ", $parameter)[1] . '}';
}