承接 nadar/prosemirror-json-parser 相关项目开发

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

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

nadar/prosemirror-json-parser

Composer 安装命令:

composer require nadar/prosemirror-json-parser

包简介

A library that easily converts ProseMirror/TipTap JSON into customizable HTML elements.

README 文档

README

The ProseMirror JSON Parser (prosemirror-json-parser) is a versatile PHP library crafted for effortless conversion of ProseMirror/TipTap JSON Model content into HTML. With dependency-free operation and exceptional parsing speed, this library ensures high-performance HTML generation. Seamlessly integrating with TipTap and ProseMirror, it offers customization through easy addition or modification of nodes. Effortlessly install via Composer, utilize the toHtml function and explore extensive customization options for tailored JSON to HTML conversion.

It functions seamlessly with both TipTap and ProseMirror because TipTap is built upon ProseMirror.

PHPUnit Tests Maintainability codecov Latest Stable Version Total Downloads License

ProseMirror JSON Parser, what AI thinks about

Key Features:

  • Dependency-free: No additional libraries required for this parser.
  • Exceptional speed: Offers high-performance parsing capabilities.
  • Highly extendible: Enables the addition of custom nodes as per your requirements.
  • 100% Code Coverage and Testing: Ensures comprehensive test coverage, guaranteeing reliability and stability.
  • Robust out-of-the-box HTML generation: Generates high-quality HTML seamlessly without requiring modifications, ensuring ease of use and reliability.

Installation & Usage

To install the library using Composer, execute the following command:

composer require nadar/prosemirror-json-parser

After installing the library, integrate the parser into your project. Utilize the toHtml function to convert your JSON value into renderable HTML code. Note that the toHtml function solely accepts an array. Therefore, if your content is in JSON format, employ json_decode($json, true) to initially convert the JSON string into an array and pass it toHtml(json_decode($json, true)).

$html = (new Nadar\ProseMirror\Parser())
    ->toHtml($json);

Extending & Customizing

Each node corresponds to a callable function within the parser, using the node's name as the key. This setup allows for easy addition or modification of nodes.

For example, to adjust the rendering of the image node, you can include your own function into the parser using the replaceNode() method:

$html = (new \Nadar\ProseMirror\Parser())
    ->replaceNode(\Nadar\ProseMirror\NodeType::image, fn(\Nadar\ProseMirror\Node $node) => '<img src="' . $node->getAttr('src') . '" class="this-is-my-class" />')
    ->toHtml($json);

To see all default nodes declared, refer to the NodeType class.

If you have a custom node with a specific name, you can add it to the parser using the addNode() method:

$html = (new \Nadar\ProseMirror\Parser())
    ->addNode('myCustomNode', fn(\Nadar\ProseMirror\Node $node) => '<div class="my-custom-node">...</div>')
    ->toHtml($json);

The addNode() and replaceNode() methods are almost identical internally, except that replaceNode should only be used when altering the output of default nodes. You can view all by-default declared nodes in the NodeType class.

Customizing Marks

In addition to customizing nodes, you can also customize how marks (like bold, italic, link, etc.) are rendered without replacing the entire text node renderer. This is particularly useful when you want to customize the output of specific marks.

For example, to customize the link mark to add additional attributes:

$html = (new \Nadar\ProseMirror\Parser())
    ->replaceMark('link', fn(\Nadar\ProseMirror\Mark $mark, string $text) => 
        '<a href="' . $mark->getAttr('href') . '" class="custom-link" rel="noopener">' . $text . '</a>'
    )
    ->toHtml($json);

You can also use the MarkType enum for type safety:

$html = (new \Nadar\ProseMirror\Parser())
    ->replaceMark(\Nadar\ProseMirror\MarkType::link, fn(\Nadar\ProseMirror\Mark $mark, string $text) => 
        '<a href="' . $mark->getAttr('href') . '" class="custom-link" rel="noopener">' . $text . '</a>'
    )
    ->toHtml($json);

You can also add custom mark renderers for your own mark types:

$html = (new \Nadar\ProseMirror\Parser())
    ->addMark('highlight', fn(\Nadar\ProseMirror\Mark $mark, string $text) => 
        '<mark class="highlight">' . $text . '</mark>'
    )
    ->toHtml($json);

Default marks that can be customized include: bold, italic, underline, strike, and link.

The addMark() and replaceMark() methods work similarly to their node counterparts - use replaceMark() for customizing default marks and addMark() for adding new custom mark types. Both methods accept either a MarkType enum or a string for the type parameter.

List Item Paragraph Handling

By default, the parser produces clean HTML for list items without wrapping content in paragraph tags: <ul><li>text</li></ul>.

If you need the previous behavior where list items were wrapped in paragraph tags (<ul><li><p>text</p></li></ul>), you can enable it:

$html = (new \Nadar\ProseMirror\Parser())
    ->setWrapParagraphsInListItems(true)
    ->toHtml($json);

Note: Paragraphs outside of list items will always be rendered with <p> tags. This option only affects paragraphs that are direct children of list items.

The text and image were enhanced by an AI, as English is not my first language and I am quite lazy. Even this sentence was generated by AI.

nadar/prosemirror-json-parser 适用场景与选型建议

nadar/prosemirror-json-parser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.12k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2023 年 11 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 nadar/prosemirror-json-parser 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 12.12k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 40
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-26