はじめに
hkob の雑記録の第424回目(通算18日目)も、NotionRubyMapping を makedown に対応させます。今日は、ページ内にページを作成する Page#create_child_page, Page#build_child_page を markdown 対応させます。
テストデータの作成
まず、テストスクリプトです。
curl 'https://api.notion.com/v1/pages' \
-H 'Authorization: Bearer '"$NOTION_API_KEY"'' \
-H "Content-Type: application/json" \
-H "Notion-Version: 2025-09-03" \
--data '{
"parent": {
"type": "page_id",
"page_id": "2f0d8e4e98ab8065b3a2ec9fd4b3e57a"
},
"markdown": "# New Page with markdown\n<meeting-notes>\n"
}'
実行しました。
> touch *.json; make
sh create_page_parent_page_with_markdown.sh > create_page_parent_page_with_markdown.json
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1158 100 991 100 167 141 23 0:00:07 0:00:07 --:--:-- 274
sleep 1
このような形でページが作成できました。今は meeting note も API で作成できるのですね。Transcription block のマニュアルも追加されていました。このブロックも後で追加しようと思います。

テストを作成
テストを追加しました。
describe "create a page with markdown" do let(:ppid) { TestConnection::POSITION_TEST_PARENT_PAGE_ID } let(:parent_page) { Page.new id: ppid } markdown = "# New Page with markdown\n<meeting-notes>\n" context "when not dry_run" do let(:target) { parent_page.create_child_page markdown: markdown } it { expect(target.id).to eq "316d8e4e98ab81489a5cdbfa4a14ef30" } it { expect(nc.hex_id(target.parent_id)).to eq ppid } end context "when dry_run" do let(:dry_run) { parent_page.create_child_page markdown: markdown, dry_run: true } it_behaves_like "dry run", :post, :pages_path, json: { "parent" => { "type" => "page_id", "page_id" => TestConnection::POSITION_TEST_PARENT_PAGE_ID, }, "markdown" => markdown, } end end
実装の作成
昨日と同様に Page#create_child_page, Page#build_child_page に markdown 引数を追加します。
# @return [NotionRubyMapping::Base] # @param [String, NilClass] template_page # @param [String, NilClass] position # @param [String, NilClass] markdown def build_child_page(template_page: nil, position: nil, markdown: nil) page = Page.new assign: [TitleProperty, "title"], parent: {"type" => "page_id", "page_id" => @id}, template_page: template_page, position: position, markdown: markdown pp = page.properties pp.clear_will_update yield page, pp if block_given? page end # @param [Boolean] dry_run true if dry_run # @return [NotionRubyMapping::Base] # @param [String, NilClass] template_page # @param [String, NilClass] position # @param [String, NilClass] markdown def create_child_page(template_page: nil, position: nil, markdown: nil, dry_run: false) page = Page.new assign: [TitleProperty, "title"], parent: {"type" => "page_id", "page_id" => @id}, template_page: template_page, position: position, markdown: markdown pp = page.properties pp.clear_will_update yield page, pp if block_given? page.save dry_run: dry_run end
WebMock の修正
昨日と同様に WebMock のテンプレートを追加します。昨日と同様に markdown の payload が与えられたら、 create_page_parent_page_with_markdown.json が読み込まれるように stubs を設定しました。
def create_page generate_stubs_sub :post, __method__, :pages_path, { (中略) parent_page_with_markdown: [nil, 200, { "parent" => { "type" => "page_id", "page_id" => TestConnection::POSITION_TEST_PARENT_PAGE_ID, }, "markdown" => "# New Page with markdown\n<meeting-notes>\n", }], } end
テスト結果は以下のようになりました。
create a page with markdown
when not dry_run
is expected to eq "316d8e4e98ab81489a5cdbfa4a14ef30"
is expected to eq "2f0d8e4e98ab8065b3a2ec9fd4b3e57a"
when dry_run
behaves like dry run
is expected to eq "#!/bin/sh\ncurl -X POST 'https://api.notion.com/v1/pages' \\\n -H 'Notion-Version: 2025-09-03' \\\n...f0d8e4e98ab8065b3a2ec9fd4b3e57a\"},\"markdown\":\"# New Page with markdown\\n<meeting-notes>\\n\"}'"
Finished in 0.1669 seconds (files took 0.61281 seconds to load)
142 examples, 0 failures
おわりに
昨日と同様に Page#create_child_page, Page#build_child_page を実装しました。明日は markdown のGET を実装します。