承接 roelmagdaleno/markdown-to-notion-blocks 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

roelmagdaleno/markdown-to-notion-blocks

Composer 安装命令:

composer require roelmagdaleno/markdown-to-notion-blocks

包简介

Convert markdown to Notion blocks.

README 文档

README

This package allows you to convert markdown to Notion blocks in JSON or Array format.

The generated Notion blocks, in JSON format, are intended to be sent to the Notion API to create a new page or update an existing one.

It's using thephpleague/commonmark under the hood to parse the Markdown.

Installation

You can install the package via composer:

composer require roelmagdaleno/markdown-to-notion-blocks

Size Limits

Maximum Blocks

The Notion API only accepts a maximum of 100 blocks per request. So, this package will always return an array, in chunks, of 100 blocks.

You'll have to send each chunk to the Notion API separately.

Text Content

The Notion API only accepts a maximum of 2000 characters per text content. If the text content is more than 2000 characters, the package will split them into multiple rich text objects.

Rich Text

The Notion API only accepts a maximum of 100 rich text objects per block. If the rich text objects are more than 100, the package will split them into multiple blocks.

Usage

This package provides a MarkdownToNotionBlocks class that you can use to convert markdown to Notion blocks in JSON or Array format.

Array

The MarkdownToNotionBlocks::array static method will return the Notion blocks in array format.

use RoelMR\MarkdownToNotionBlocks\MarkdownToNotionBlocks;

$markdown = '# Hello, world!';
$notionBlocks = MarkdownToNotionBlocks::array($markdown);

print_r($notionBlocks);

The code above will output the following array:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [object] => block
                    [type] => heading_1
                    [heading_1] => Array
                        (
                            [rich_text] => Array
                                (
                                    [0] => Array
                                        (
                                            [type] => text
                                            [text] => Array
                                                (
                                                    [content] => Hello, world!
                                                    [link] => 
                                                )
                                            [annotations] => Array
                                                (
                                                    [bold] => 
                                                    [italic] => 
                                                    [strikethrough] => 
                                                    [underline] => 
                                                    [code] => 
                                                    [color] => default
                                                )
                                        )
                                )
                            [color] => default
                            [is_toggleable] => 
                        )
                )
        )
)

JSON

The MarkdownToNotionBlocks::json static method will return the Notion blocks in JSON format.

Note

Since the Notion blocks are returned in chunks of 100, the JSON output might not be valid for the Notion API. The JSON output is useful for debugging purposes or send it from the server to the client.

use RoelMR\MarkdownToNotionBlocks\MarkdownToNotionBlocks;

$markdown = '# Hello, world!';
$notionBlocks = MarkdownToNotionBlocks::json($markdown);

echo $notionBlocks;

The code above will output the following JSON:

[
  [
    {
      "object": "block",
      "type": "heading_1",
      "heading_1": {
        "rich_text": [
          {
            "type": "text",
            "text": {
              "content": "Hello, world!",
              "link": null
            },
            "annotations": {
              "bold": false,
              "italic": false,
              "strikethrough": false,
              "underline": false,
              "code": false,
              "color": "default"
            }
          }
        ],
        "color": "default",
        "is_toggleable": false
      }
    }
  ]
]

Transform Output

If you want to transform the output before sending to the Notion API, you can use the MarkdownToNotionBlocks::array method to get the Notion blocks in array format and then apply your transformations.

For example, by default each Notion block has a color property set to default. If you want to change the color of the heading to red, you can do the following:

use RoelMR\MarkdownToNotionBlocks\MarkdownToNotionBlocks;

$markdown = '# Hello, world!';

$notionBlocks = MarkdownToNotionBlocks::array($markdown);

$notionBlocks[0]['heading_1']['rich_text'][0]['text']['content'] = 'My heading changed.';
$notionBlocks[0]['heading_1']['color'] = 'red';

echo json_encode($notionBlocks);

After applying the transformation, encode the array to JSON and send it to the Notion API.

Supported Notion Blocks

The following Notion blocks are supported by this package:

Each block support rich text properties like bold, italic, strikethrough, underline, and inline code.

Example

The following example shows how to convert a Markdown string to Notion blocks in Array format.

use RoelMR\MarkdownToNotionBlocks\MarkdownToNotionBlocks;

$markdown = file_get_contents($file_path);

$notion_blocks = MarkdownToNotionBlocks::array($markdown);

if (!$notion_blocks) {
    return false;
}

$notion_db_id = '101c7zs03d0680e6aa1cf27a0e61e8f4';

foreach ($notion_blocks as $notion_block) {
    append_blocks_to_notion_page($notion_db_id, $notion_block);
}

Testing

composer test

roelmagdaleno/markdown-to-notion-blocks 适用场景与选型建议

roelmagdaleno/markdown-to-notion-blocks 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 125 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 09 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「markdown」 「blocks」 「notion」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 roelmagdaleno/markdown-to-notion-blocks 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 roelmagdaleno/markdown-to-notion-blocks 我们能提供哪些服务?
定制开发 / 二次开发

基于 roelmagdaleno/markdown-to-notion-blocks 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 125
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-09-15