oblik / kirby-outsource
3.0.0
2022-10-06 10:18 UTC
Requires
- getkirby/cms: ^3.4
- getkirby/composer-installer: ^1.1
- league/html-to-markdown: ^4.8
Requires (Dev)
- getkirby/editor: ^1.0
- oblik/kirby-link-field: ^3.0
- phpunit/phpunit: ^8
This package is auto-updated.
Last update: 2024-03-20 11:50:42 UTC
README
插件,允许您根据指定的蓝图遍历、导出和导入所有站点数据。它是为了作为其他插件的依赖项而创建的。功能
- 按字段序列化内容(在结构中解析YAML,将逗号分隔的标签字段转换为数组等)
- 将KirbyTags转换为XML并返回(供不理解KirbyTag语法的另一个API消费)
- 将Markdown转换为HTML并返回(供不处理Markdown的API使用)
- 导出整个站点、页面和文件模型
- 使用与导出相同的模式导入内容
- 根据其
id
字段遍历块和结构(如果它们有此类字段) - PHP类易于扩展,以实现自定义行为
- 支持已弃用的Kirby Editor
安装
从oblik/kirby-walker on Packagist使用Composer
composer require oblik/kirby-walker
使用方法
导出
您可以使用Exporter
类导出内容
use Oblik\Walker\Walker\Exporter; $exporter = new Exporter(); $result = $exporter->walk(page('home')); echo json_encode($result, JSON_PRETTY_PRINT);
{ "title": "Home", "headline": "Welcome to Kirby's Starterkit", "subheadline": "A fully documented example project" }
导入
您可以使用Importer
类将输入内容与当前模型内容合并。
use Oblik\Walker\Walker\Importer; $importer = new Importer(); $model = page('home'); $result = $importer->walk($model, [ 'input' => [ 'headline' => 'Updated headline!' ] ]); echo json_encode($data, JSON_PRETTY_PRINT);
{ "title": "Home", "headline": "Updated headline!", "subheadline": "A fully documented example project" }
合并数据后,您可以使用生成的数组对模型应用更改
kirby()->impersonate('kirby'); $model->update($result);
选项
有几个选项允许您将原始内容转换为更适合其他系统的格式。然后,插件可以将重新格式化的内容转换回其原始格式,同时保留对其所做的任何更改。
解析KirbyTags
将KirbyTags转换为XML
Subheadline: A fully (link: https://getkirby.com/docs/guide text: documented target: _blank) example project
$exporter->walk(page('home'), [ 'options' => [ 'parseKirbyTags' => true ] ]);
{ "subheadline": "A fully <kirby link=\"https://getkirby.com/docs/guide\" text=\"documented\" target=\"_blank\"/> example project" }
默认情况下,KirbyTag属性被编码为XML属性。如果消费系统需要操作这些值(例如翻译它们),您可能需要将它们的内容从标签中移出。而不是这个
<kirby link="https://getkirby.com/docs/guide" text="documented" target="_blank"/>
…你可能需要这个
<kirby link="https://getkirby.com/docs/guide" target="_blank"> <value name="text" index="1">documented</value> </kirby>
要这样做,只需使用externalAttributes
设置
$exporter->walk(page('home'), [ 'options' => [ 'parseKirbyTags' => [ 'externalAttributes' => ['text'] ] ] ]);
{ "subheadline": "A fully <kirby link=\"https://getkirby.com/docs/guide\" target=\"_blank\"><value name=\"text\" index=\"1\">documented</value></kirby> example project" }
解析Markdown
如果消费系统不理解Markdown,您可以将其转换为HTML
Text:
# Hello World
This is a fully **documented** example project!
$exporter->walk(page('home'), [ 'options' => [ 'parseMarkdown' => true ] ]);
{ "text": "<h1>Hello World</h1><p>This is a fully <strong>documented</strong> example project!</p>" }
注意:这仅适用于textarea
字段类型。
解析模板
将大括号内的内容转换为XML
Title: {{ site.title }} Home
$exporter->walk(page('home'), [ 'options' => [ 'parseTemplates' => true ] ]);
{ "title": "<meta template=\" site.title \"/> Home" }
扩展
您可以通过扩展基本类轻松添加自定义行为。例如,您可以像这样返回每个字段的字符长度
use Oblik\Walker\Walker\Walker; class CustomWalker extends Walker { protected function walkText(string $text, $context) { return strlen($text); } } $walker = new CustomWalker(); $data = $walker->walk(page('home')); echo json_encode($data, JSON_PRETTY_PRINT);
{ "title": 4, "headline": 29, "subheadline": 34 }
您也可以使用每个字段的蓝图中的值
class CustomWalker extends Walker { protected function walkField(Field $field, $context) { return $context['blueprint']['label'] ?? 'NONE'; } }
{ "title": "NONE", "headline": "Headline", "gap": "Gap", "subheadline": "Subheadline" }
酷的地方在于,解析结构字段的YAML和布局和块字段的JSON已经处理好了。您的自定义逻辑将自动在所有嵌套字段中工作。
字段类型的独立逻辑
您可以通过添加walkField{{ field type }}
方法为不同的字段类型提供不同的逻辑。例如,您可以通过添加walkFieldGap
来更改仅针对gap
字段的行为
class CustomWalker extends Walker { protected function walkFieldGap($field, $context) { return 'Mind the gap!'; } }
{ "title": "Home", "headline": "Welcome to Kirby's Starterkit", "gap": "Mind the gap!", "subheadline": "A fully documented example project" }