GithubHelp home page GithubHelp logo

dc7290 / microcms-richedit-processer Goto Github PK

View Code? Open in Web Editor NEW
23.0 0.0 1.0 968 KB

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

License: MIT License

JavaScript 23.87% TypeScript 75.51% Shell 0.61%
microcms typescript

microcms-richedit-processer's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.