notion_ruby_mapping 0.6.3 → 0.6.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b295b0ba6b110a2f9e2f559e251490005a446abd71d2b7cc67903f9b0e2e504a
4
- data.tar.gz: 8b6d1ed3f4c53973c8391981cef7044628ba22894dd42f8b23364f9a995326d9
3
+ metadata.gz: c1a2aa2a4c8880559f3a314967335cfd4f008b9884890111b6d5edfcb0f6251c
4
+ data.tar.gz: 52ece830f67e3373f653f60dcee81b1076c5ab095eb9d53bb4d0e3340c76bc7c
5
5
  SHA512:
6
- metadata.gz: 5d3e718b49fceea38ee34be30161496d9023158631b24a633408af4b9eb5f5507ae85484c74a4f0094a7e1d8aaa47c851cc2d47af128e45814d98a5c1f5a21f4
7
- data.tar.gz: 95b5374a44768b1454d550abcad6343c06d24333d787045d5317ba91ef0a1f0a1ddb273879ca28e928e2808180b7acbd144a4e8f19c52cb7208d565083d19906
6
+ metadata.gz: 7b70e9092468ef61744ec120e499402daf214e35d5c86158d4b44fc3fb9d7262e6d5b82e6763b20554f843996a9d614bd181b09baad66f2533fa1a076b15555b
7
+ data.tar.gz: e618b6066d6d17e4564cdfbadda3b3f0d7b598740f5b3ed1c2a9c2b640d28284cfa8ecfc1676fb0de6be0d604cd24670856ce112e049fbe5a91ac44b4404b05b
data/README.md CHANGED
@@ -75,19 +75,16 @@ NotionCache.instance.create_client ENV["NOTION_API_TOKEN"] # from environment
75
75
  ### 2.3 Sample codes
76
76
 
77
77
  1. [Database and page access sample](https://www.notion.so/hkob/Database-and-page-access-sample-d30033e707194faf995741167eb2b6f8)
78
- 1. [Append block children sample](https://www.notion.so/hkob/Append-block-children-sample-3867910a437340be931cf7f2c06443c6)
79
- 1. [Update block sample](https://www.notion.so/hkob/update-block-sample-5568c1c36fe84f12b83edfe2dda83028)
78
+ 2. [Append block children sample](https://www.notion.so/hkob/Append-block-children-sample-3867910a437340be931cf7f2c06443c6)
79
+ 3. [Update block sample](https://www.notion.so/hkob/update-block-sample-5568c1c36fe84f12b83edfe2dda83028)
80
80
 
81
81
  ### 2.4. Another example code (Use case)
82
82
 
83
83
  1. [Set icon to all icon unsettled pages](examples/set_icon_to_all_icon_unsettled_pages.md)
84
- 1. [Renumbering pages](examples/renumbering_pages.md)
85
- 1. [Change title](examples/change_title.md)
86
- 1. [Create erDiagram from Notion database](tools/createErDiagram.rb)
87
- ```shell
88
- Usage:
89
- ruby tools/createErDiagram.rb database_id code_block_id
90
- ```
84
+ 2. [Renumbering pages](examples/renumbering_pages.md)
85
+ 3. [Change title](examples/change_title.md)
86
+ 4. [Create ER Diagram from Notion database](https://www.notion.so/hkob/notionErDiagram-Sample-1720c2199c534ca08138cde38f31f710)
87
+ 5. [Create Sitemap from Notion pages](https://www.notion.so/hkob/NotionSitemap-sample-14e195c83d024c5382aab09210916c87)
91
88
 
92
89
  ### 2.5 API reference
93
90
 
@@ -95,6 +92,9 @@ Usage:
95
92
 
96
93
  ## 3. ChangeLog
97
94
 
95
+ - 2022/8/10 [v0.6.6] Bug fix(notionSitemap.rb): Skip if child page is empty.
96
+ - 2022/8/10 [v0.6.5] add notionSitemap.rb, rename createErDiagram to notionErDiagram, and move them to exe directory
97
+ - 2022/8/9 [v0.6.4] url can be entered instead of page_id, block_id and database_id
98
98
  - 2022/8/9 [v0.6.3] update createErDiagram.rb (Fixed a bug with non-ASCII database titles)
99
99
  - 2022/8/7 [v0.6.2] add comment_object support
100
100
  - 2022/7/28 [v0.6.1] added createErDiagram.rb
@@ -6,13 +6,13 @@ include NotionRubyMapping
6
6
  def append_database(text, db, db_titles)
7
7
  base_title = db_title db
8
8
  normalize_db_title(db, db_titles) if db_titles[db].nil?
9
- text << "#{db_titles[db]} {"
10
- text << %( Database title "#{base_title}") unless base_title == db_titles[db]
9
+ text << " #{db_titles[db]} {"
10
+ text << %( Database title "#{base_title}") unless base_title == db_titles[db]
11
11
  db.properties.reject { |p| p.is_a? RelationProperty }.each_with_index do |p, i|
12
12
  class_name = p.class.name.split("::").last.sub /Property/, ""
13
- text << %( #{class_name} p#{i} "#{p.name}")
13
+ text << %( #{class_name} p#{i} "#{p.name}")
14
14
  end
15
- text << "}\n"
15
+ text << " }\n"
16
16
  end
17
17
 
18
18
  def normalize_db_title(db, db_titles)
@@ -25,7 +25,7 @@ def db_title(db)
25
25
  end
26
26
 
27
27
  if ARGV.length < 2
28
- print "Usage: createErDiagram.rb top_database_id code_block_id"
28
+ print "Usage: notionErDiagram.rb top_database_id code_block_id"
29
29
  exit
30
30
  end
31
31
  database_id, code_block_id = ARGV
@@ -47,11 +47,24 @@ until dbs.empty?
47
47
  db.properties.select { |pp| pp.is_a? RelationProperty }.each_with_index do |pp, i|
48
48
  new_db = Database.find pp.relation_database_id
49
49
  normalize_db_title(new_db, db_titles) if db_titles[new_db].nil?
50
- text << "#{db_titles[db]} |o--o{ #{db_titles[new_db]} : r#{i}"
50
+ text << " #{db_titles[db]} |o--o{ #{db_titles[new_db]} : r#{i}"
51
51
  dbs << new_db unless finished[new_db]
52
52
  end
53
53
  text << ""
54
54
  end
55
- block.rich_text_array.rich_text_objects = text.join("\n ")
55
+
56
+ text_objects = text.each_with_object([]) do |str, ans|
57
+ strn = "#{str}\n"
58
+ if (last = ans.last)
59
+ if last.length + strn.length > 1999
60
+ ans << strn
61
+ else
62
+ ans[-1] += strn
63
+ end
64
+ else
65
+ ans << strn
66
+ end
67
+ end
68
+ block.rich_text_array.rich_text_objects = text_objects
56
69
  block.language = "mermaid"
57
70
  block.save
@@ -0,0 +1,131 @@
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
@@ -25,7 +25,7 @@ module NotionRubyMapping
25
25
  assign.each_slice(2) { |(klass, key)| assign_property(klass, key) }
26
26
  @json ||= {}
27
27
  end
28
- attr_reader :json, :id, :archived
28
+ attr_reader :json, :id, :archived, :has_children
29
29
 
30
30
  # @param [Hash, Notion::Messages] json
31
31
  # @return [NotionRubyMapping::Base]
@@ -44,6 +44,22 @@ module NotionRubyMapping
44
44
  end
45
45
  end
46
46
 
47
+ def self.block_id(str)
48
+ if /^http/.match str
49
+ /#([\da-f]{32})/.match(str)[1]
50
+ else
51
+ NotionCache.instance.hex_id str
52
+ end
53
+ end
54
+
55
+ def self.database_id(str)
56
+ if /^http/.match str
57
+ /([\da-f]{32})\?/.match(str)[1]
58
+ else
59
+ NotionCache.instance.hex_id str
60
+ end
61
+ end
62
+
47
63
  # @param [Object] method
48
64
  # @param [Object] path
49
65
  # @param [nil] json
@@ -58,21 +74,29 @@ module NotionRubyMapping
58
74
  shell.join(" \\\n")
59
75
  end
60
76
 
77
+ def self.page_id(str)
78
+ if /^http/.match str
79
+ /([\da-f]{32})$/.match(str)[1]
80
+ else
81
+ NotionCache.instance.hex_id str
82
+ end
83
+ end
84
+
61
85
  def comments(query = nil, dry_run: false)
62
- if page? || block?
63
- if dry_run
64
- self.class.dry_run_script :get, @nc.retrieve_comments_path(@id)
65
- else
66
- ans = {}
67
- List.new(comment_parent: self,
68
- json: @nc.retrieve_comments_request(@id, query),
69
- query: query).each do |comment|
70
- dt_id = comment.discussion_id
71
- dt = ans[dt_id] ||= DiscussionThread.new dt_id
72
- dt.comments << comment
73
- end
74
- ans
86
+ return unless page? || block?
87
+
88
+ if dry_run
89
+ self.class.dry_run_script :get, @nc.retrieve_comments_path(@id)
90
+ else
91
+ ans = {}
92
+ List.new(comment_parent: self,
93
+ json: @nc.retrieve_comments_request(@id, query),
94
+ query: query).each do |comment|
95
+ dt_id = comment.discussion_id
96
+ dt = ans[dt_id] ||= DiscussionThread.new dt_id
97
+ dt.comments << comment
75
98
  end
99
+ ans
76
100
  end
77
101
  end
78
102
 
@@ -67,10 +67,11 @@ module NotionRubyMapping
67
67
  # @return [NotionRubyMapping::Block]
68
68
  def self.find(id, dry_run: false)
69
69
  nc = NotionCache.instance
70
+ block_id = Base.block_id id
70
71
  if dry_run
71
- Base.dry_run_script :get, nc.block_path(id)
72
+ Base.dry_run_script :get, nc.block_path(block_id)
72
73
  else
73
- nc.block id
74
+ nc.block block_id
74
75
  end
75
76
  end
76
77
 
@@ -10,10 +10,11 @@ module NotionRubyMapping
10
10
  # @see https://www.notion.so/hkob/Database-1462b24502424539a4231bedc07dc2f5#58ba9190fd544432a9e2a5823d6c33b7
11
11
  def self.find(id, dry_run: false)
12
12
  nc = NotionCache.instance
13
+ database_id = Base.database_id id
13
14
  if dry_run
14
- dry_run_script :get, nc.database_path(id)
15
+ dry_run_script :get, nc.database_path(database_id)
15
16
  else
16
- nc.database id
17
+ nc.database database_id
17
18
  end
18
19
  end
19
20
 
@@ -25,9 +25,9 @@ module NotionRubyMapping
25
25
  def block_json(not_update: true)
26
26
  ans = super
27
27
  ans[type] = if @page_id
28
- {"type" => "page_id", "page_id" => @page_id}
28
+ {"type" => "page_id", "page_id" => Base.page_id(@page_id)}
29
29
  else
30
- {"type" => "database_id", "database_id" => @database_id}
30
+ {"type" => "database_id", "database_id" => Base.database_id(@database_id)}
31
31
  end
32
32
  ans
33
33
  end
@@ -10,10 +10,11 @@ module NotionRubyMapping
10
10
  # @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#7d868b8b81c3473082bbdc7370813a4a
11
11
  def self.find(id, dry_run: false)
12
12
  nc = NotionCache.instance
13
+ page_id = Base.page_id id
13
14
  if dry_run
14
- Base.dry_run_script :get, nc.page_path(id)
15
+ Base.dry_run_script :get, nc.page_path(page_id)
15
16
  else
16
- nc.page id
17
+ nc.page page_id
17
18
  end
18
19
  end
19
20
 
@@ -11,7 +11,7 @@ module NotionRubyMapping
11
11
  synced_from = @json[type]["synced_from"]
12
12
  @block_id = synced_from && @nc.hex_id(synced_from["block_id"])
13
13
  else
14
- @block_id = block_id
14
+ @block_id = Base.block_id block_id
15
15
  add_sub_blocks sub_blocks
16
16
  end
17
17
  @can_have_children = @block_id.nil?
@@ -31,6 +31,11 @@ module NotionRubyMapping
31
31
  @will_update = true
32
32
  end
33
33
 
34
+ # @return [String (frozen)]
35
+ def text
36
+ @expression
37
+ end
38
+
34
39
  protected
35
40
 
36
41
  # @return [Hash{String (frozen)->String}]
@@ -12,6 +12,11 @@ module NotionRubyMapping
12
12
  @options["plain_text"] = url
13
13
  end
14
14
 
15
+ # @return [String, NilClass]
16
+ def page_id
17
+ @options["page_id"]
18
+ end
19
+
15
20
  # @return [String (frozen)]
16
21
  def text
17
22
  ""
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NotionRubyMapping
4
- VERSION = "0.6.3"
4
+ VERSION = "0.6.6"
5
5
  NOTION_VERSION = "2022-06-28"
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notion_ruby_mapping
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 0.6.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroyuki KOBAYASHI
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-09 00:00:00.000000000 Z
11
+ date: 2022-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -125,7 +125,9 @@ dependencies:
125
125
  description: Mapping tool from Notion Database/Page/Block to Ruby Objects.
126
126
  email:
127
127
  - hkob@metro-cit.ac.jp
128
- executables: []
128
+ executables:
129
+ - notionErDiagram.rb
130
+ - notionSitemap.rb
129
131
  extensions: []
130
132
  extra_rdoc_files: []
131
133
  files:
@@ -144,6 +146,8 @@ files:
144
146
  - examples/change_title.md
145
147
  - examples/renumbering_pages.md
146
148
  - examples/set_icon_to_all_icon_unsettled_pages.md
149
+ - exe/notionErDiagram.rb
150
+ - exe/notionSitemap.rb
147
151
  - images/post_set_icon.png
148
152
  - images/pre_set_icon.png
149
153
  - images/serial_number.png
@@ -234,7 +238,6 @@ files:
234
238
  - notion_ruby_mapping.gemspec
235
239
  - sig/notion_ruby_mapping.rbs
236
240
  - tools/an
237
- - tools/createErDiagram.rb
238
241
  homepage: https://github.com/hkob/notion_ruby_mapping.git
239
242
  licenses:
240
243
  - MIT