hugo-notion 0.1.0.pre.alpha.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +3 -0
- data/lib/hugo_notion/synchronizer.rb +126 -0
- data/lib/hugo_notion/version.rb +3 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a6bd4925e3b3a59530ce6240621fffc48526af50e7437d8fa1c412920a247a72
|
4
|
+
data.tar.gz: 3ce5c9c0170acaddade872e954566180a4b8bb5d0b29be542ac836e0d8ad6f15
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dbe275da44f5a7bde42180a20e889338935e1c3299c20bfc96860521220f0f394c887201c60bea65427b34ef224696329b49c017aaa1562f0e4994e9e9513f25
|
7
|
+
data.tar.gz: 87aa5d782b3461a21b95ac0e30a702f6b91fc7dadc7a8c779ce88ad6a07a37e7d408aad6ec02c824a32434539434afc02e21f752b408f7198a19a03a0d9bf4de
|
data/README.md
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
env_file_path = File.expand_path('../../.env', File.dirname(__FILE__))
|
2
|
+
if File.exist?(env_file_path)
|
3
|
+
require 'dotenv'
|
4
|
+
Dotenv.load(env_file_path)
|
5
|
+
end
|
6
|
+
notion_token = ENV['NOTION_TOKEN']
|
7
|
+
unless notion_token
|
8
|
+
throw "Please create a Notion integration, generate a secret and provide it in the 'NOTION_TOKEN' environment variable"
|
9
|
+
end
|
10
|
+
|
11
|
+
NOTION_TOKEN = ENV['NOTION_TOKEN']
|
12
|
+
|
13
|
+
require 'httparty'
|
14
|
+
require 'notion-ruby-client'
|
15
|
+
require 'notion_to_md'
|
16
|
+
require 'yaml'
|
17
|
+
require 'pry'
|
18
|
+
|
19
|
+
class NotionApi
|
20
|
+
BASE_URL = "https://api.notion.com/v1"
|
21
|
+
|
22
|
+
def initialize(api_secret)
|
23
|
+
@api_secret = api_secret
|
24
|
+
end
|
25
|
+
|
26
|
+
def get(path)
|
27
|
+
url = File.join(BASE_URL, path)
|
28
|
+
HTTParty.get(url, headers: {
|
29
|
+
"Notion-Version" => '2022-02-22',
|
30
|
+
"Authorization" => "Bearer #{@api_secret}"
|
31
|
+
})
|
32
|
+
end
|
33
|
+
|
34
|
+
def post(path)
|
35
|
+
url = File.join(BASE_URL, path)
|
36
|
+
HTTParty.post(url, headers: {
|
37
|
+
"Notion-Version" => '2022-02-22',
|
38
|
+
"Authorization" => "Bearer #{@api_secret}"
|
39
|
+
})
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class Synchronizer
|
44
|
+
class << self
|
45
|
+
def run(options = {})
|
46
|
+
if options[:notion_page_id]
|
47
|
+
notion_page_id = options[:notion_page_id]
|
48
|
+
elsif options[:notion_database_id]
|
49
|
+
notion_database_id = options[:notion_database_id]
|
50
|
+
end
|
51
|
+
destination_dir = options[:destination_dir]
|
52
|
+
Dir.mkdir(destination_dir) unless Dir.exist?(destination_dir)
|
53
|
+
|
54
|
+
notion = NotionApi.new(NOTION_TOKEN)
|
55
|
+
response = if notion_database_id
|
56
|
+
notion.post("/databases/#{notion_database_id}/query")
|
57
|
+
elsif notion_page_id
|
58
|
+
notion.get("/blocks/#{notion_page_id}/children")
|
59
|
+
end
|
60
|
+
notion_blocks = response['results']
|
61
|
+
|
62
|
+
existing_page_file_names = Dir.entries(destination_dir).select { |f| !File.directory? f }
|
63
|
+
page_file_names = []
|
64
|
+
notion_blocks.each do |notion_block|
|
65
|
+
notion_block_id = notion_notion_block['id']
|
66
|
+
|
67
|
+
if notion_block['type'] == 'child_database'
|
68
|
+
notion_child_database_title = notion_block['child_database']['title']
|
69
|
+
run(
|
70
|
+
notion_database_id: notion_block_id,
|
71
|
+
destination_dir: File.join(destination_dir, notion_child_database_title)
|
72
|
+
)
|
73
|
+
next
|
74
|
+
end
|
75
|
+
|
76
|
+
block_last_edited_at = Time.parse(notion_block['last_edited_time'])
|
77
|
+
|
78
|
+
page_front_matter = {
|
79
|
+
'date' => Time.parse(notion_block['created_time'])
|
80
|
+
}
|
81
|
+
if notion_block['properties']
|
82
|
+
if block.dig('properties', 'date', 'date', 'start')
|
83
|
+
page_front_matter['date'] = Time.parse(notion_block['properties']['date']['date']['start'])
|
84
|
+
end
|
85
|
+
|
86
|
+
if block.dig('properties', 'Name', 'title', 0, 'plain_text')
|
87
|
+
page_front_matter['title'] = notion_block['properties']['Name']['title'][0]['plain_text']
|
88
|
+
end
|
89
|
+
end
|
90
|
+
if notion_block['type'] == 'child_page'
|
91
|
+
page_front_matter['title'] = notion_block['child_page']['title']
|
92
|
+
page_front_matter['type'] = notion_block['child_page']['title']
|
93
|
+
end
|
94
|
+
page_title = page_front_matter['title']
|
95
|
+
|
96
|
+
# Ignore incomplete notion pages
|
97
|
+
next unless page_front_matter['date']
|
98
|
+
next unless page_front_matter['title']
|
99
|
+
|
100
|
+
page_file_name_without_extension = page_title.gsub(' ', '-')
|
101
|
+
page_file_name = "#{page_file_name_without_extension}.md"
|
102
|
+
page_file_names << page_file_name
|
103
|
+
|
104
|
+
page_path = File.join(destination_dir, page_file_name)
|
105
|
+
unless ENV['NOTION_SYNCHRONIZER_SKIP_OPTIMIZATIONS']
|
106
|
+
next if File.exist?(page_path) && File.mtime(page_path) >= block_last_edited_at
|
107
|
+
end
|
108
|
+
|
109
|
+
page_front_matter_yaml = page_front_matter.to_yaml.chomp
|
110
|
+
page_markdown = NotionToMd.convert(notion_page_id: notion_block_id, token: NOTION_TOKEN)
|
111
|
+
page_content = <<-page_CONTENT
|
112
|
+
#{page_front_matter_yaml}
|
113
|
+
---
|
114
|
+
|
115
|
+
#{page_markdown}
|
116
|
+
page_CONTENT
|
117
|
+
File.write(page_path, page_content)
|
118
|
+
end
|
119
|
+
old_page_file_names = existing_page_file_names - page_file_names
|
120
|
+
old_page_file_names.each do |file_name|
|
121
|
+
file_path = File.join(destination_dir, file_name)
|
122
|
+
File.delete(file_path) if File.exist?(file_path) && !File.directory?(file_path)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hugo-notion
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0.pre.alpha.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nisanth Chunduru
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-04-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: notion_to_md
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: yaml
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Write in Notion. Publish with Hugo.
|
70
|
+
email:
|
71
|
+
- nisanth074@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- README.md
|
77
|
+
- lib/hugo_notion/synchronizer.rb
|
78
|
+
- lib/hugo_notion/version.rb
|
79
|
+
homepage: https://github.com/nisanthchunduru/hugo-notion
|
80
|
+
licenses: []
|
81
|
+
metadata: {}
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: 1.3.1
|
96
|
+
requirements: []
|
97
|
+
rubygems_version: 3.4.19
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Write in Notion. Publish with Hugo.
|
101
|
+
test_files: []
|