Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

microcms-richedit-processer

dc7290355MIT1.2.0TypeScript support: included

microCMSのリッチエディタから取得した値を加工したり、目次リストを作成します。

microCMS, richedit, preprocesser

readme

microcms-richedit-processer

npm version license downloads

microCMSのリッチエディタから取得できるHTMLデータを自動処理します。

Feature

HTML データの加工機能について

  • img タグ
    • 遅延読み込み(現在、遅延読み込みライブラリのlazysizesのクラス名をサポート)
    • レスポンシブ画像のサポート(srcSet, sizes 属性によってウインドウ幅に合わせて最適な画像を配信する技術)
    • placeholder 画像の設定
    • imgix パラメータの追加
    • width, height 属性の自動設定
  • iframe タグ
    • 遅延読み込み(現在、遅延読み込みライブラリのlazysizesのクラス名をサポート)
    • レスポンシブ対応
  • pre タグ内の code タグ
    • シンタックスハイライトのためにクラス名の追加(現在、highlight.jsをサポート)
  • 共通
    • クラス名の追加
    • 任意の属性値の追加

HTML データから別データの自動作成機能について

  • 目次リストの作成

Usage

npm i microcms-richedit-processer
# yarn add microcms-richedit-processer

Next.jsの場合

import { GetStaticProps, NextPage } from "next";

import { createTableOfContents, processer } from "microcms-richedit-processer";

type Props = {
  body: string;
  toc: {
    id: string;
    text: string;
    name: string;
  }[];
};

export const getStaticProps: GetStaticProps<Props> = async () => {
  const { contents } = await fetch(
    "https://{サービスID}.microcms.io/api/v1/{エンドポイント}",
    {
      headers: {
        "X-API-KEY": "{APIキー}",
      },
    }
  ).then((res) => res.json());

  // contents.bodyにHTMLデータが取得できる想定です。

  return {
    props: {
      body: await processer(contents.body),
      // オプションを渡す場合
      // body: processer(contents.body, {}),
      toc: createTableOfContents(contents.body),
      // オプションを渡す場合
      // toc: createTableOfContents(contents.body, {}),
    },
  };
};

詳しい使い方はZennの記事に書いています! https://zenn.dev/d_suke/articles/e18352797bbe00bdabb6

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

Released

1.2.0 (2023-01-15)

Features

  • 🚀 Update dependency node-html-parser to v6 (bf94926)

Bug Fixes

  • 🐛 Fixed error in res.buffer (862eddb), closes #192
  • 🐛 Update dependency image-size to v1.0.2 (9e9319a)

Continuous Integration

  • 🎡 Added semantic-release (ab637db)
  • 🎡 Change toke name (0e94898)
  • 🎡 setup semantic-release-cli (637c241)
  • 🎡 Update ESLint settings (9f13d4e)

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

[Released]

1.1.0 (2022-05-24)

Features

  • 🚀Other elements can be handled (b6511f6)

1.0.6 (2022-05-19)

Fixed

  • update dependencies and devDependencies

1.0.5 (2022-03-20)

Fixed

  • update dependencies and devDependencies

1.0.4 (2021-10-03)

Fixed

  • update dependencies and devDependencies

1.0.3 (2021-08-11)

Fixed

  • update dependencies and devDependencies
  • update dependencies: example-nextjs (70367df)

1.0.2 (2021-07-24)

Fixed

  • update dependencies and devDependencies (061df85)
  • update dependencies and devDependencies: example-nextjs (150bd82)

1.0.1 (2021-07-09)

Fixed

  • img: Placeholder オプションにおいて、data-src が生成されるのを修正(43573f9)

1.0.0 (2021-07-09)

Added

  • Everything!