HeadingNBlock: NotionRubyMapping 解説 (17)

はじめに

NotionRubyMapping 解説の第17回目です。今日もほぼこれまでと同様の形式を持つ Heading1Block, Heading2Block, Heading3Block を解説します。今日紹介する Heading block も TextSubBlockColorBaseBlock というクラスの子クラスです。今日の作業ページはこちらです。

hkob.notion.site

あらかじめページを取得しておきます。

irb(main):002> page = Page.find "https://hkob.notion.site/HeadingNBlock-17-7049c767be4d475fbb4a182f79c4a66b?pvs=4"
=> NotionRubyMapping::Page-7049c767be4d475fbb4a182f79c4a66b

Heading1Block

Heading1Block は見出し1を示すテキストブロックです。 text_infocolor が設定できるのは ParagraphBlock と同様です。いつものように text_info のみを設定したらエラーになっていました。

irb(main):003> h1 = page.append_block_children Heading1Block.new("H1")
/Users/hkob/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/notion_ruby_mapping-0.8.3/lib/notion_ruby_mapping/blocks/base.rb:142:in `append_block_children': {"object"=>"error", "status"=>400, "code"=>"validation_error", "message"=>"body failed validation: body.children[0].heading_1.color should be `\"default\"`, `\"gray\"`, `\"brown\"`, `\"orange\"`, `\"yellow\"`, `\"green\"`, `\"blue\"`, `\"purple\"`, `\"pink\"`, `\"red\"`, `\"gray_background\"`, `\"brown_background\"`, `\"orange_background\"`, `\"yellow_background\"`, `\"green_background\"`, `\"blue_background\"`, `\"purple_background\"`, `\"pink_background\"`, `\"red_background\"`, or `undefined`, instead was `null`.", "request_id"=>"6f56ec81-c470-4b9e-8f20-df74cccc818b"} (StandardError)
    from (irb):3:in `<main>'
  from <internal:kernel>:187:in `loop'
  from /Users/hkob/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/irb-1.11.1/exe/irb:9:in `<top (required)>'
    from /Users/hkob/.rbenv/versions/3.3.0/bin/irb:25:in `load'
  from /Users/hkob/.rbenv/versions/3.3.0/bin/irb:25:in `<main>'

color でエラーになっているようです。color を設定したら無事に作成されました。

irb(main):004> h1 = page.append_block_children Heading1Block.new("H1", color:
"red")
=> NotionRubyMapping::Heading1Block-091e1dde2cb143368c87f9afada42838

実行した結果以下のような見出しが用意されました。

H1

ソースを確認したところ、以下のようになっていました。他の TextSubBlockColorBaseBlock のサブクラスは color: "default" になっていたのですが、Heading1Block は nil になっていました。後で直しておきます。

  class Heading1Block < TextSubBlockColorBaseBlock
    # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>, nil] text_info
    # @param [String] color
    def initialize(text_info = nil, color: nil, json: nil, id: nil, parent: nil)
      super(text_info, color: color, json: json, id: id, parent: parent)
      @can_have_children = false
    end

Heading1BlockTextSubBlockColorBaseBlock のサブクラスなのですが、sub_blocks は設定できません。sub_blocks が設定できるトグル付きのブロック ToggleHeading1Block は明日説明します。これだけだと短すぎるので、見出し2、見出し3 も同じように作ってみましょう。

irb(main):007> h2 = page.append_block_children Heading2Block.new("H2", color:
"green")
=> NotionRubyMapping::Heading2Block-076901ec59924bcea356480d7597d32c
irb(main):008> h3 = page.append_block_children Heading3Block.new("H3", color:
"orange")
=> NotionRubyMapping::Heading3Block-c2f534a6efcd4ee09da18ce72ffe43cc

結果は以下のようになりました。

H1H2H3

TextSubBlockColorBaseBlock の子クラスなので、color などの設定ができます。

irb(main):011> h1.color = "default"
=> "default"
irb(main):012> h1.save
=> NotionRubyMapping::Heading1Block-091e1dde2cb143368c87f9afada42838

h1 は色を変えるとテキストの色も変わるようです。

H1color

おわりに

今回は HeadingNBlock を解説してみました。TextSubBlockColorBaseBlock の子クラスなので、これまで解説していたものと同じ機能があることがわかります。

Heading1Block のマニュアルはこちら。

hkob.notion.site

NotionRubyMapping解説