candysax/telegraph-node-converter
最新稳定版本:1.0.1
Composer 安装命令:
composer require candysax/telegraph-node-converter
包简介
Convert HTML to the content of the Telegraph page and back.
README 文档
README
This library helps with the content field of the Page Telegraph API object. It converts an HTML string or a DOMDocument into the format required by the Telegraph API, and also converts it back to an HTML string or a DOMDocument.
- Telegraph API Docs: https://telegra.ph/api
Installation
To install this library run the command:
composer require candysax/telegraph-node-converter
Usage
Converting from HTML to an array of Node
As the source data for conversion, the convertToNode method accepts a string containing html tags or a DOMDocument object. The result can be obtained as an array of Node or its representation as a JSON string.
$nodes = HTML::convertToNode('<p>Hello <b>world</b></p>');
Get as array:
$nodes->array();
Array
(
[0] => Array
(
[tag] => p
[children] => Array
(
[0] => Hello
[1] => Array
(
[tag] => b
[children] => Array
(
[0] => world
)
)
)
)
)
Get as JSON:
$nodes->json();
'[{"tag":"p","children":["Hello ",{"tag":"b","children":["world"]}]}]'
Passing the DOMDocument object as a data source for conversion:
$dom = new DOMDocument(); $dom->loadHTML('<p>Hello world <a href="https://example.com/">link</a></p>'); $nodes = HTML::convertToNode($dom)->json();
Converting from an array of Node to HTML
As the source data for the conversion, the convertToHtml method accepts an array of Node or a JSON string. The result can be obtained in the form of a string with HTML tags or a DOM object.
$html = Node::convertToHtml([ [ 'tag' => 'p', 'children' => [ 'Hello ', [ 'tag' => 'b', 'children' => [ 'world', ], ], ], ], ]);
Get as string:
$html->string();
'<p>Hello <b>world</b></p>'
Get as DOMDocument:
$html->dom();
DOMDocument Object
Multiple conversions
$input = '<p>Hello <b>world</b> <a href="https://example.com/">link</a></p>'; HTML::convertToNode($input)->convertToHtml()->convertToNode()->convertToHtml()->string();
Examples
Creating a Telegraph page:
use GuzzleHttp\Client; use Candysax\TelegraphNodeConverter\HTML; function createPage() { $client = new Client(); $client->request('POST', 'https://api.telegra.ph/createPage', [ 'form_params' => [ 'access_token' => 'your_telegraph_token', 'title' => 'Example', 'content' => HTML::convertToNode( '<p>Hello world <a href="https://example.com/">link</a></p>' )->json(), ], ]); }
Getting the content of the Telegraph page:
use GuzzleHttp\Client; use Candysax\TelegraphNodeConverter\HTML; function getPageContent() { $client = new Client(); $response = $client->request('POST', 'https://api.telegra.ph/getPage', [ 'form_params' => [ 'path' => 'path_to_the_telegraph_page', 'return_content' => true, ], ])->getBody(); $data = json_decode($response, true); return Node::convertToHtml($data['result']['content'])->string(); }
Testing
Tests can be launched by running the following:
composer test
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 65
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-09-06