notion_ruby_mapping 2.0.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +16 -6
  3. data/exe/notionErDiagram.rb +30 -24
  4. data/lib/notion_ruby_mapping/blocks/base.rb +32 -4
  5. data/lib/notion_ruby_mapping/blocks/block.rb +0 -1
  6. data/lib/notion_ruby_mapping/blocks/column_block.rb +7 -2
  7. data/lib/notion_ruby_mapping/blocks/data_source.rb +167 -0
  8. data/lib/notion_ruby_mapping/blocks/database.rb +41 -21
  9. data/lib/notion_ruby_mapping/blocks/file_base_block.rb +3 -3
  10. data/lib/notion_ruby_mapping/blocks/list.rb +5 -5
  11. data/lib/notion_ruby_mapping/controllers/mermaid.rb +13 -13
  12. data/lib/notion_ruby_mapping/controllers/{mermaid_database.rb → mermaid_data_source.rb} +39 -39
  13. data/lib/notion_ruby_mapping/controllers/notion_cache.rb +42 -7
  14. data/lib/notion_ruby_mapping/controllers/property_cache.rb +26 -4
  15. data/lib/notion_ruby_mapping/objects/file_upload_object.rb +1 -1
  16. data/lib/notion_ruby_mapping/properties/checkbox_property.rb +1 -1
  17. data/lib/notion_ruby_mapping/properties/created_by_property.rb +2 -2
  18. data/lib/notion_ruby_mapping/properties/created_time_property.rb +1 -1
  19. data/lib/notion_ruby_mapping/properties/date_property.rb +1 -1
  20. data/lib/notion_ruby_mapping/properties/files_property.rb +2 -2
  21. data/lib/notion_ruby_mapping/properties/formula_property.rb +4 -4
  22. data/lib/notion_ruby_mapping/properties/last_edited_by_property.rb +2 -2
  23. data/lib/notion_ruby_mapping/properties/last_edited_time_property.rb +1 -1
  24. data/lib/notion_ruby_mapping/properties/multi_select_property.rb +4 -4
  25. data/lib/notion_ruby_mapping/properties/number_property.rb +4 -4
  26. data/lib/notion_ruby_mapping/properties/people_property.rb +2 -2
  27. data/lib/notion_ruby_mapping/properties/phone_number_property.rb +1 -1
  28. data/lib/notion_ruby_mapping/properties/property.rb +23 -5
  29. data/lib/notion_ruby_mapping/properties/relation_property.rb +26 -14
  30. data/lib/notion_ruby_mapping/properties/rich_text_property.rb +1 -1
  31. data/lib/notion_ruby_mapping/properties/rollup_property.rb +7 -7
  32. data/lib/notion_ruby_mapping/properties/select_property.rb +5 -5
  33. data/lib/notion_ruby_mapping/properties/text_property.rb +1 -1
  34. data/lib/notion_ruby_mapping/properties/title_property.rb +1 -1
  35. data/lib/notion_ruby_mapping/properties/unique_id_property.rb +1 -1
  36. data/lib/notion_ruby_mapping/properties/url_property.rb +1 -1
  37. data/lib/notion_ruby_mapping/version.rb +2 -2
  38. data/lib/notion_ruby_mapping.rb +3 -3
  39. data/tools/an +4 -4
  40. metadata +3 -9
  41. data/exe/erdToNotionDb.rb +0 -56
  42. data/exe/notionSitemap.rb +0 -131
  43. data/exe/notionTimeRecorder.rb +0 -284
  44. data/lib/notion_ruby_mapping/blocks/template_block.rb +0 -34
@@ -30,14 +30,14 @@ module NotionRubyMapping
30
30
 
31
31
  # @param [String] new_name
32
32
  def new_name=(new_name)
33
- assert_database_property __method__
33
+ assert_data_source_property __method__
34
34
  @will_update = true
35
35
  @new_name = new_name
36
36
  end
37
37
 
38
38
  # @return [NotionRubyMapping::Property] self
39
39
  def remove
40
- assert_database_property __method__
40
+ assert_data_source_property __method__
41
41
  @will_update = true
42
42
  @remove = true
43
43
  self
@@ -143,6 +143,14 @@ module NotionRubyMapping
143
143
  @base_type == "database"
144
144
  end
145
145
 
146
+ def data_source?
147
+ @base_type == "data_source"
148
+ end
149
+
150
+ def database_or_data_source?
151
+ %w[database data_source].include? @base_type
152
+ end
153
+
146
154
  # @return [TrueClass, FalseClass] true if it has Property contents
147
155
  def contents?
148
156
  !instance_of? Property
@@ -188,15 +196,25 @@ module NotionRubyMapping
188
196
  raise StandardError, "#{method} can execute only Database property." unless database?
189
197
  end
190
198
 
199
+ # @param [Symbol, nil] method
200
+ def assert_database_or_data_source_property(method)
201
+ raise StandardError, "#{method} can execute only Database property." if page?
202
+ end
203
+
191
204
  # @return [Hash]
192
205
  def property_schema_json
193
- assert_database_property __method__
206
+ assert_database_or_data_source_property __method__
194
207
  {@name => {type => property_schema_json_sub}}
195
208
  end
196
209
 
210
+ ## DataSource property only methods
211
+ def assert_data_source_property(method)
212
+ raise StandardError, "#{method} can execute only DataSource property." unless data_source?
213
+ end
214
+
197
215
  # @return [Hash]
198
216
  def update_property_schema_json
199
- assert_database_property __method__
217
+ assert_database_or_data_source_property __method__
200
218
  if @remove
201
219
  {@name => nil}
202
220
  elsif @new_name
@@ -210,7 +228,7 @@ module NotionRubyMapping
210
228
 
211
229
  # @param [Symbol, nil] method
212
230
  def assert_page_property(method)
213
- raise StandardError, "#{method} can execute only Page property." if database?
231
+ raise StandardError, "#{method} can execute only Page property." if database? || data_source?
214
232
  end
215
233
 
216
234
  # @return [NotionRubyMapping::Property, Array<UserObject>, nil]
@@ -44,20 +44,20 @@ module NotionRubyMapping
44
44
 
45
45
  ## Database property only methods
46
46
 
47
- # @return [String] relation database_id
47
+ # @return [String] relation data_source_id
48
48
  # @see https://www.notion.so/hkob/RelationProperty-f608ab41a1f0476b98456620346fba03#eb40f1a2ad5c4e368d343870a7e529f9
49
- def relation_database_id
50
- assert_database_property __method__
51
- @json["database_id"]
49
+ def relation_data_source_id
50
+ assert_data_source_property __method__
51
+ @json["data_source_id"]
52
52
  end
53
53
 
54
- # @param [String] database_id
54
+ # @param [String] data_source_id
55
55
  # @param [String] synced_property_name
56
56
  # @see https://www.notion.so/hkob/RelationProperty-f608ab41a1f0476b98456620346fba03#7f5029fb7f6e4c009f22888b233e6f64
57
- def replace_relation_database(database_id: nil, type: "dual_property")
58
- assert_database_property __method__
57
+ def replace_relation_data_source(data_source_id: nil, type: "dual_property")
58
+ assert_database_or_data_source_property __method__
59
59
  @will_update = true
60
- @json["database_id"] = database_id if database_id
60
+ @json["data_source_id"] = data_source_id if data_source_id
61
61
  @json["type"] = type
62
62
  @json[type] = {}
63
63
  @json.delete type == "dual_property" ? "single_property" : "dual_property"
@@ -75,7 +75,7 @@ module NotionRubyMapping
75
75
  property_cache: nil, query: nil)
76
76
  super name, will_update: will_update, base_type: base_type, property_id: property_id,
77
77
  property_cache: property_cache, query: query
78
- @json = if database?
78
+ @json = if database_or_data_source?
79
79
  json || {}
80
80
  elsif relation
81
81
  Array(relation).map { |r| {"id" => r} }
@@ -88,7 +88,7 @@ module NotionRubyMapping
88
88
 
89
89
  # @return [Hash]
90
90
  def update_property_schema_json
91
- assert_database_property __method__
91
+ assert_database_or_data_source_property __method__
92
92
  ans = super
93
93
  return ans if ans != {} || !@will_update
94
94
 
@@ -97,16 +97,28 @@ module NotionRubyMapping
97
97
  ans
98
98
  end
99
99
 
100
- def database_id
101
- @json["database_id"]
100
+ def data_source_id
101
+ @json["data_source_id"]
102
+ end
103
+
104
+ # @return [TrueClass, FalseClass] true if dual_property
105
+ def dual_property?
106
+ assert_database_or_data_source_property __method__
107
+ @json["type"] == "dual_property"
108
+ end
109
+
110
+ # @return [TrueClass, FalseClass] true if single_property
111
+ def single_property?
112
+ assert_database_or_data_source_property __method__
113
+ @json["type"] == "single_property"
102
114
  end
103
115
 
104
116
  def synced_property_id
105
- @json["type"] == "dual_property" ? @json["dual_property"]["synced_property_id"] : nil
117
+ dual_property? ? @json["dual_property"]["synced_property_id"] : nil
106
118
  end
107
119
 
108
120
  def synced_property_name
109
- @json["type"] == "dual_property" ? @json["dual_property"]["synced_property_name"] : nil
121
+ dual_property? ? @json["dual_property"]["synced_property_name"] : nil
110
122
  end
111
123
 
112
124
  # @return [Hash] created json
@@ -12,7 +12,7 @@ module NotionRubyMapping
12
12
  # @param [Hash] json
13
13
  def update_from_json(json)
14
14
  @will_update = false
15
- if database?
15
+ if database_or_data_source?
16
16
  @json = json["rich_text"] || {}
17
17
  else
18
18
  @text_objects = RichTextArray.new "rich_text", json: json["rich_text"]
@@ -23,14 +23,14 @@ module NotionRubyMapping
23
23
  # @return [String] new or settled function
24
24
  # @see https://www.notion.so/hkob/RollupProperty-eb10fbac3a93436289e74e5c651e9134#94021d4e6c0b44519443c1e1cc6b3aba
25
25
  def function
26
- assert_database_property __method__
26
+ assert_database_or_data_source_property __method__
27
27
  @json["function"]
28
28
  end
29
29
 
30
30
  # @param [String] func
31
31
  # @see https://www.notion.so/hkob/RollupProperty-eb10fbac3a93436289e74e5c651e9134#647c74803cac49a9b79199828157e17a
32
32
  def function=(func)
33
- assert_database_property __method__
33
+ assert_database_or_data_source_property __method__
34
34
  @will_update = true
35
35
  @json["function"] = func
36
36
  end
@@ -38,14 +38,14 @@ module NotionRubyMapping
38
38
  # @return [String] new or settled relation_property_name
39
39
  # @see https://www.notion.so/hkob/RollupProperty-eb10fbac3a93436289e74e5c651e9134#684fc4739c4f4d6a9b93687f72cd8dad
40
40
  def relation_property_name
41
- assert_database_property __method__
41
+ assert_database_or_data_source_property __method__
42
42
  @json["relation_property_name"]
43
43
  end
44
44
 
45
45
  # @param [String] rpn
46
46
  # @see https://www.notion.so/hkob/RollupProperty-eb10fbac3a93436289e74e5c651e9134#a61c2100758841c381edd820aa88ac65
47
47
  def relation_property_name=(rpn)
48
- assert_database_property __method__
48
+ assert_database_or_data_source_property __method__
49
49
  @will_update = true
50
50
  @json["relation_property_name"] = rpn
51
51
  end
@@ -53,14 +53,14 @@ module NotionRubyMapping
53
53
  # @return [String] new or settled rollup_property_name
54
54
  # @see https://www.notion.so/hkob/RollupProperty-eb10fbac3a93436289e74e5c651e9134#8ce9ee31a2e2473ab7ba21781e4b440d
55
55
  def rollup_property_name
56
- assert_database_property __method__
56
+ assert_database_or_data_source_property __method__
57
57
  @json["rollup_property_name"]
58
58
  end
59
59
 
60
60
  # @param [String] rpn
61
61
  # @see https://www.notion.so/hkob/RollupProperty-eb10fbac3a93436289e74e5c651e9134#66503f4472f2456aa9b383f03b1fe0a6
62
62
  def rollup_property_name=(rpn)
63
- assert_database_property __method__
63
+ assert_database_or_data_source_property __method__
64
64
  @will_update = true
65
65
  @json["rollup_property_name"] = rpn
66
66
  end
@@ -82,7 +82,7 @@ module NotionRubyMapping
82
82
 
83
83
  # @return [Hash]
84
84
  def update_property_schema_json
85
- assert_database_property __method__
85
+ assert_database_or_data_source_property __method__
86
86
  ans = super
87
87
  return ans if ans != {} || !@will_update
88
88
 
@@ -38,7 +38,7 @@ module NotionRubyMapping
38
38
 
39
39
  # @return [Array] copyed multi select options
40
40
  def edit_select_options
41
- assert_database_property __method__
41
+ assert_database_or_data_source_property __method__
42
42
  @will_update = true
43
43
  @json["options"] ||= []
44
44
  end
@@ -46,14 +46,14 @@ module NotionRubyMapping
46
46
  # @return [Array]
47
47
  # @see https://www.notion.so/hkob/SelectProperty-6d6a0defa70d4b26af0fdbdcfbf99f28#790a297f2c1b4ba5a4d86074d4c70a89
48
48
  def select_options
49
- assert_database_property __method__
49
+ assert_database_or_data_source_property __method__
50
50
  @json["options"] || []
51
51
  end
52
52
 
53
53
  # @return [String]
54
54
  # @see https://www.notion.so/hkob/SelectProperty-6d6a0defa70d4b26af0fdbdcfbf99f28#72da1632793c4a5296f3bc89de2df413
55
55
  def select_names
56
- assert_database_property __method__
56
+ assert_database_or_data_source_property __method__
57
57
  @json["options"].map { |s| s["name"] }
58
58
  end
59
59
 
@@ -78,7 +78,7 @@ module NotionRubyMapping
78
78
  property_cache: nil)
79
79
  super name, will_update: will_update, base_type: base_type, property_id: property_id,
80
80
  property_cache: property_cache
81
- @json = if database?
81
+ @json = if database_or_data_source?
82
82
  json || {"options" => []}
83
83
  else
84
84
  json || {"name" => select}
@@ -89,7 +89,7 @@ module NotionRubyMapping
89
89
 
90
90
  # @return [Hash]
91
91
  def update_property_schema_json
92
- assert_database_property __method__
92
+ assert_database_or_data_source_property __method__
93
93
  ans = super
94
94
  return ans if ans != {} || !@will_update
95
95
 
@@ -33,7 +33,7 @@ module NotionRubyMapping
33
33
 
34
34
  super name, will_update: will_update, base_type: base_type, property_id: property_id,
35
35
  property_cache: property_cache, query: query
36
- @text_objects = if database?
36
+ @text_objects = if database_or_data_source?
37
37
  json || {}
38
38
  else
39
39
  RichTextArray.new "title", json: json, text_objects: text_objects
@@ -21,7 +21,7 @@ module NotionRubyMapping
21
21
  # @param [Hash] json
22
22
  def update_from_json(json)
23
23
  @will_update = false
24
- if database?
24
+ if database_or_data_source?
25
25
  @json = json || {}
26
26
  else
27
27
  @text_objects = TitleProperty.rich_text_array_from_json json
@@ -30,7 +30,7 @@ module NotionRubyMapping
30
30
  def initialize(name, will_update: false, base_type: "page", json: nil, property_id: nil, property_cache: nil)
31
31
  super name, will_update: will_update, base_type: base_type, property_id: property_id, property_cache: property_cache
32
32
  @json = json
33
- @json ||= {} if database?
33
+ @json ||= {} if database_or_data_source?
34
34
  end
35
35
 
36
36
  # @param [Hash] json
@@ -37,7 +37,7 @@ module NotionRubyMapping
37
37
  def initialize(name, will_update: false, base_type: "page", json: nil, property_id: nil, property_cache: nil)
38
38
  super name, will_update: will_update, base_type: base_type, property_id: property_id,
39
39
  property_cache: property_cache
40
- @json = json || (database? ? {} : nil)
40
+ @json = json || (database_or_data_source? ? {} : nil)
41
41
  end
42
42
 
43
43
  ## Page property only methods
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NotionRubyMapping
4
- VERSION = "2.0.1"
5
- NOTION_VERSION = "2022-06-28"
4
+ VERSION = "3.0.0"
5
+ NOTION_VERSION = "2025-09-03"
6
6
  end
@@ -4,16 +4,16 @@ require "yaml"
4
4
 
5
5
  require_relative "notion_ruby_mapping/version"
6
6
  {
7
- blocks: %w[base block database list page url_caption_base_block bookmark_block breadcrumb_block
7
+ blocks: %w[base block database data_source list page url_caption_base_block bookmark_block breadcrumb_block
8
8
  text_sub_block_color_base_block bulleted_list_item_block callout_block child_base_block
9
9
  child_database_block child_page_block code_block column_list_block column_block divider_block
10
10
  embed_block equation_block file_base_block file_block heading1_block heading2_block heading3_block
11
11
  image_block toggle_heading1_block toggle_heading2_block toggle_heading3_block url_base_block
12
12
  link_preview_block link_to_page_block numbered_list_item_block paragraph_block pdf_block quote_block
13
- synced_block table_block table_row_block table_of_contents_block template_block to_do_block
13
+ synced_block table_block table_row_block table_of_contents_block to_do_block
14
14
  toggle_block video_block],
15
15
  controllers: %w[notion_cache payload property_cache query rich_text_array discussion_thread search mermaid
16
- mermaid_database],
16
+ mermaid_data_source],
17
17
  objects: %w[rich_text_object emoji_object equation_object file_object mention_object text_object user_object
18
18
  comment_object file_upload_object],
19
19
  properties: %w[property checkbox_property multi_property created_by_property date_base_property created_time_property
data/tools/an CHANGED
@@ -8,7 +8,7 @@ module NotionRubyMapping
8
8
  # you Notion API token
9
9
  NOTION_API_TOKEN = "secret_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx"
10
10
  # your task Database id
11
- DATABASE_ID = "0123456789abcdef0123456789abcdef"
11
+ DATA_SOURCE_ID = "0123456789abcdef0123456789abcdef"
12
12
  # your Title name
13
13
  TITLE_NAME = "タスク名"
14
14
  # your DateProperty name
@@ -75,10 +75,10 @@ module NotionRubyMapping
75
75
  usage
76
76
  end
77
77
 
78
- NotionCache.instance.create_client NOTION_API_TOKEN
78
+ NotionRubyMapping.configure { |c| c.token = NOTION_API_TOKEN }
79
79
 
80
- # create database object without API call and create a child page
81
- db = Database.new id: DATABASE_ID
80
+ # create data_source object without API call and create a child page
81
+ DataSource.new id: DATA_SOURCE_ID
82
82
  page = db.create_child_page TitleProperty, TITLE_NAME, DateProperty, DATE_NAME
83
83
 
84
84
  # obtain title and date properties and set values
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notion_ruby_mapping
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroyuki KOBAYASHI
@@ -153,10 +153,7 @@ description: Mapping tool from Notion Database/Page/Block to Ruby Objects.
153
153
  email:
154
154
  - hkob@metro-cit.ac.jp
155
155
  executables:
156
- - erdToNotionDb.rb
157
156
  - notionErDiagram.rb
158
- - notionSitemap.rb
159
- - notionTimeRecorder.rb
160
157
  extensions: []
161
158
  extra_rdoc_files: []
162
159
  files:
@@ -175,10 +172,7 @@ files:
175
172
  - examples/change_title.md
176
173
  - examples/renumbering_pages.md
177
174
  - examples/set_icon_to_all_icon_unsettled_pages.md
178
- - exe/erdToNotionDb.rb
179
175
  - exe/notionErDiagram.rb
180
- - exe/notionSitemap.rb
181
- - exe/notionTimeRecorder.rb
182
176
  - images/post_set_icon.png
183
177
  - images/pre_set_icon.png
184
178
  - images/serial_number.png
@@ -195,6 +189,7 @@ files:
195
189
  - lib/notion_ruby_mapping/blocks/code_block.rb
196
190
  - lib/notion_ruby_mapping/blocks/column_block.rb
197
191
  - lib/notion_ruby_mapping/blocks/column_list_block.rb
192
+ - lib/notion_ruby_mapping/blocks/data_source.rb
198
193
  - lib/notion_ruby_mapping/blocks/database.rb
199
194
  - lib/notion_ruby_mapping/blocks/divider_block.rb
200
195
  - lib/notion_ruby_mapping/blocks/embed_block.rb
@@ -217,7 +212,6 @@ files:
217
212
  - lib/notion_ruby_mapping/blocks/table_block.rb
218
213
  - lib/notion_ruby_mapping/blocks/table_of_contents_block.rb
219
214
  - lib/notion_ruby_mapping/blocks/table_row_block.rb
220
- - lib/notion_ruby_mapping/blocks/template_block.rb
221
215
  - lib/notion_ruby_mapping/blocks/text_sub_block_color_base_block.rb
222
216
  - lib/notion_ruby_mapping/blocks/to_do_block.rb
223
217
  - lib/notion_ruby_mapping/blocks/toggle_block.rb
@@ -229,7 +223,7 @@ files:
229
223
  - lib/notion_ruby_mapping/blocks/video_block.rb
230
224
  - lib/notion_ruby_mapping/controllers/discussion_thread.rb
231
225
  - lib/notion_ruby_mapping/controllers/mermaid.rb
232
- - lib/notion_ruby_mapping/controllers/mermaid_database.rb
226
+ - lib/notion_ruby_mapping/controllers/mermaid_data_source.rb
233
227
  - lib/notion_ruby_mapping/controllers/notion_cache.rb
234
228
  - lib/notion_ruby_mapping/controllers/payload.rb
235
229
  - lib/notion_ruby_mapping/controllers/property_cache.rb
data/exe/erdToNotionDb.rb DELETED
@@ -1,56 +0,0 @@
1
- #! /usr/bin/env ruby
2
-
3
- require "notion_ruby_mapping"
4
- include NotionRubyMapping
5
-
6
- if ARGV.length < 2
7
- print "Usage: erdToNotion.rb code_block_id target_page_id [--inline]"
8
- exit
9
- end
10
-
11
- mermaid_url, target_page_url, inline_flag = ARGV
12
- inline = inline_flag == "--inline"
13
- NotionRubyMapping.configure { |c| c.notion_token = ENV["NOTION_API_KEY"] }
14
- nc = NotionCache.instance
15
- mermaid_block = Block.find mermaid_url
16
- unless mermaid_block.is_a? CodeBlock
17
- print "#{mermaid_url} is not CodeBlock's id"
18
- exit
19
- end
20
-
21
- # Parse mermaid ER
22
- mermaid = Mermaid.new mermaid_block.rich_text_array.full_text
23
-
24
- # Retrieve child databases
25
- target_page = Page.find target_page_url
26
- target_page_id = target_page.id
27
- db_titles = target_page.children.select { |b| b.is_a? ChildDatabaseBlock }.map(&:title)
28
- exist_dbs = Search.new(query: "(#{db_titles.join('|')})", database_only: true).exec.select do |db|
29
- nc.hex_id(db.parent_id) == target_page_id
30
- end
31
-
32
- # Attach existing databases to mermaid database
33
- exist_dbs.each do |db|
34
- mermaid.attach_database db
35
- end
36
-
37
- mermaid.create_notion_db target_page, inline
38
- mermaid.update_title
39
- pass = 1
40
- pre_remain = nil
41
- loop do
42
- print "=== Pass #{pass} ===\n"
43
- mermaid.update_databases
44
- mermaid.rename_reverse_name
45
- count = mermaid.count
46
- remain = mermaid.remain
47
- print "Pass #{pass} Remain #{remain} in #{count}\n"
48
- if pre_remain == remain
49
- print "Dependency loop detected.\n"
50
- exit
51
- end
52
- pre_remain = remain
53
- break if remain.zero?
54
- pass += 1
55
- end
56
- print "Finish!!!"
data/exe/notionSitemap.rb DELETED
@@ -1,131 +0,0 @@
1
- #! /usr/bin/env ruby
2
-
3
- require "notion_ruby_mapping"
4
- include NotionRubyMapping
5
-
6
- # see https://zenn.dev/kinkinbeer135ml/articles/f08ce790091aca
7
- def escape_title(title)
8
- title.gsub /[(){}\[\]!”#$%&’()=^〜|¥1234567890@`「」{};:+*<>、。・?_]/, ""
9
- end
10
-
11
- class Sitemap
12
- def initialize(top_page, orient, nolink)
13
- @top_page = top_page
14
- @code = {@top_page => "p0"}
15
- @queue = [@top_page]
16
- @finished = {}
17
- @page_links = Hash.new { |h, k| h[k] = [] }
18
- @text = ["flowchart #{orient}"]
19
- @nolink = nolink
20
- end
21
- attr_reader :text
22
-
23
- def dig_pages
24
- until @queue.empty?
25
- page = @queue.shift
26
- @finished[page] = true
27
- search_blocks(page)
28
- @text << %(click #{@code[page]} "#{page["url"]}")
29
- end
30
- end
31
-
32
- def code_with_title(page)
33
- "#{@code[page]}(#{escape_title page.title})"
34
- end
35
-
36
- def search_blocks(page)
37
- print "#{page.title}\n"
38
- @children = []
39
- @block_queue = page.children.to_a
40
- search_block page, @block_queue.shift until @block_queue.empty?
41
- unless @children.empty?
42
- title = page == @top_page ? code_with_title(page) : @code[page]
43
- @text << [title, @children.map { |p| code_with_title p }.join(" & ")].join(" --> ")
44
- end
45
- print "\n"
46
- end
47
-
48
- def search_block(page, block)
49
- case block
50
- when ChildPageBlock
51
- child_block = block.children.first
52
- add_child child_block.parent if child_block
53
- when LinkToPageBlock
54
- add_link page, block.page_id unless @nolink
55
- else
56
- @block_queue += block.children.to_a if block.has_children
57
- search_link_in_rta(page, block.rich_text_array) if !@nolink && block.is_a?(TextSubBlockColorBaseBlock)
58
- end
59
- end
60
-
61
- def add_child(child)
62
- @queue << child
63
- @children << child
64
- set_code child
65
- print " --> #{child.title}\n"
66
- end
67
-
68
- def set_code(page)
69
- @code[page] ||= "p#{@code.length}"
70
- end
71
-
72
- def add_link(page, link_page_id)
73
- return unless link_page_id
74
-
75
- begin
76
- link_page = Page.find link_page_id
77
- rescue
78
- print "\n#{link_page_id} can not read by this integration\n"
79
- return
80
- end
81
- set_code link_page
82
- @page_links[page] << link_page
83
- print " -.-> #{link_page.title} "
84
- end
85
-
86
- def search_link_in_rta(page, rta)
87
- rta.each { |to| add_link page, to.page_id if to.is_a?(MentionObject) && to.page_id }
88
- end
89
-
90
- def link_pages
91
- @page_links.each do |org, array|
92
- link_finished = {}
93
- array.each do |lp|
94
- link_finished[lp] = @finished[lp] ? @code[lp] : code_with_title(lp)
95
- end
96
- @text << [@code[org], link_finished.values.join(" & ")].join(" -.-> ")
97
- end
98
- end
99
- end
100
-
101
- if ARGV.length < 3
102
- print "Usage: notionSitemap.rb top_page_id code_block_id orient(LR or TD) [--nolink]"
103
- exit
104
- end
105
- top_page_id, code_block_id, orient, nolink = ARGV
106
- NotionCache.instance.create_client ENV["NOTION_API_KEY"]
107
- code_block = Block.find code_block_id
108
- unless code_block.is_a? CodeBlock
109
- print "#{code_block_id} is not CodeBlock's id"
110
- exit
111
- end
112
-
113
- top_page = Page.find top_page_id
114
- sm = Sitemap.new top_page, orient, nolink == "--nolink"
115
- sm.dig_pages
116
- sm.link_pages unless nolink
117
- text_objects = sm.text.each_with_object([]) do |str, ans|
118
- strn = "#{str}\n"
119
- if (last = ans.last)
120
- if last.length + strn.length > 1999
121
- ans << strn
122
- else
123
- ans[-1] += strn
124
- end
125
- else
126
- ans << strn
127
- end
128
- end
129
- code_block.rich_text_array.rich_text_objects = text_objects
130
- code_block.language = "mermaid"
131
- code_block.save