CodeBlock: NotionRubyMapping 解説 (31)

はじめに

NotionRubyMapping 解説の第31回目です。最後に確認したところ、CodeBlock の紹介を忘れていたことに気づきました。今日の作業ページはこちらです。

hkob.notion.site

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

irb(main):002> page = Page.find "https://hkob.notion.site/CodeBlock-31-9356b10b51cc4dd9ba65ee58e64dc836?pvs=4"
=> NotionRubyMapping::Page-9356b10b51cc4dd9ba65ee58e64dc836

CodeBlock

CodeBlock は中のテキストと、言語、キャプションを指定します。

irb(main):003> cb = page.append_block_children CodeBlock.new("irb", language: "s
hell", caption: "対話的Rubyの実行")

実行した結果以下のようなコードブロックが作成されています。中身のテキスト、言語、キャプションが正しく設定されていることがわかります。

CodeBlock with full option
CodeBlock に引数を指定しないで追加した場合には空の CodeBlock が生成されます。

irb(main):004> cb2 = page.append_block_children CodeBlock.new
=> NotionRubyMapping::CodeBlock-4f15dc05b01543bdab7c00150a611b3b

実行すると以下のようになりました。言語は事前に設定したものが引き継がれるのではないかと思います。こちらも Shell になりました。

CodeBlock without option

中のテキストは rich_text_array を変更することができます。

irb(main):007> cb2.rich_text_array << "include NotionRubyMapping"
=>
#<NotionRubyMapping::TextObject:0x00000001254782b0
 @options={"plain_text"=>"include NotionRubyMapping"},
 @text="include NotionRubyMapping",
 @type="text",
 @will_update=false>
irb(main):008> cb2.save
=> NotionRubyMapping::CodeBlock-4f15dc05b01543bdab7c00150a611b3bi

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

Repace code

言語は ruby なので、 language= で変更します。

irb(main):009> cb2.language = "ruby"
=> "ruby"
irb(main):010> cb2.save
=> NotionRubyMapping::CodeBlock-4f15dc05b01543bdab7c00150a611b3b

言語が変更されていることがわかります。Ruby の構文を認識したので、include の部分で色が変わっていることがわかります。

Change language

せっかくなので、caption も変更してみます。

irb(main):011> cb2.caption << "NotionRubyMapping の Mix-in"
=>
#<NotionRubyMapping::TextObject:0x00000001258169a8
 @options={"plain_text"=>"NotionRubyMapping の Mix-in"},
 @text="NotionRubyMapping の Mix-in",
 @type="text",
 @will_update=false>
irb(main):012> cb2.save
=> NotionRubyMapping::CodeBlock-4f15dc05b01543bdab7c00150a611b3b

正しく、caption が設定されたことがわかりました。

Add caption

おわりに

今回は一つ忘れていた CodeBlock を解説してみました。language を設定することで言語に合わせた色付けがされます。

CodeBlock のマニュアルはこちらです。

CodeBlock

NotionRubyMapping解説