bridgetown_notion 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/CHANGELOG.md +11 -1
- data/Gemfile +3 -0
- data/README.md +31 -14
- data/bridgetown_notion.gemspec +1 -0
- data/lib/bridgetown_notion/builder.rb +10 -67
- data/lib/bridgetown_notion/markdown_generators/bulleted_list_item_markdown_generator.rb +11 -0
- data/lib/bridgetown_notion/markdown_generators/heading_1_markdown_generator.rb +11 -0
- data/lib/bridgetown_notion/markdown_generators/heading_2_markdown_generator.rb +11 -0
- data/lib/bridgetown_notion/markdown_generators/heading_3_markdown_generator.rb +11 -0
- data/lib/bridgetown_notion/markdown_generators/heading_4_markdown_generator.rb +11 -0
- data/lib/bridgetown_notion/markdown_generators/heading_5_markdown_generator.rb +11 -0
- data/lib/bridgetown_notion/markdown_generators/heading_6_markdown_generator.rb +11 -0
- data/lib/bridgetown_notion/markdown_generators/paragraph_markdown_generator.rb +11 -0
- data/lib/bridgetown_notion/notion_posts_factory.rb +43 -0
- data/lib/bridgetown_notion/parsers/block_parser.rb +25 -0
- data/lib/bridgetown_notion/parsers/categories_parser.rb +12 -0
- data/lib/bridgetown_notion/parsers/content_parser.rb +27 -0
- data/lib/bridgetown_notion/parsers/is_published_parser.rb +11 -0
- data/lib/bridgetown_notion/parsers/post_parser.rb +28 -0
- data/lib/bridgetown_notion/parsers/published_at_parser.rb +11 -0
- data/lib/bridgetown_notion/parsers/slug_parser.rb +13 -0
- data/lib/bridgetown_notion/parsers/tags_parser.rb +12 -0
- data/lib/bridgetown_notion/parsers/title_parser.rb +11 -0
- data/lib/bridgetown_notion/version.rb +1 -1
- data/lib/bridgetown_notion.rb +12 -0
- metadata +34 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 54259116b5b744f64ed97d979eb96a7b5fc32908e101ebceb966e5ff850f2614
|
4
|
+
data.tar.gz: 6766dd525ac714f6cdc49410148204f124a34bd21a600bd591cb2aff54e37d64
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d5d2d274bc28e7965666163509781ebf62f92f10911f4d2ad0b909240531fc7a16e814c6dc687fdb7733c22a28c42134b57129317bee2414f8344401616da0e1
|
7
|
+
data.tar.gz: '04903b167f5f58f59ef1f2558c4a296212d15138236dc4ef5b3e5c965979c5ca652ce1f0151d9cea6c60dcda25abd4c7103b1a42f13eeefec3d561116be04f7b'
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -7,7 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
7
7
|
|
8
8
|
## [Unreleased]
|
9
9
|
|
10
|
-
-
|
10
|
+
## [0.2.0] - 2023-03-13
|
11
|
+
|
12
|
+
### Added
|
13
|
+
|
14
|
+
- Add new initializer config, backwards compatible with previous env implementation.
|
15
|
+
- Add activesupport dependecy for help with changing letter cases.
|
16
|
+
|
17
|
+
### Fixed
|
18
|
+
|
19
|
+
- Fix invalid URI bug. By returning a slug property from the posts factory we can ensure
|
20
|
+
that we don't attempt to create posts with spaces in the file name.
|
11
21
|
|
12
22
|
## [0.1.0] - 2023-02-04
|
13
23
|
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,34 +1,51 @@
|
|
1
|
-
#
|
1
|
+
# Bridgetown Notions CMS Plugin
|
2
2
|
|
3
|
-
|
3
|
+
A Bridgetown plugin to pull content directly out of a Notion database. Each page in the
|
4
|
+
Notion database is added to your posts collection in Bridgetown.
|
4
5
|
|
5
|
-
_You can run_ `bridgetown plugins new` _to easily set up a customized verison of this starter repo._
|
6
|
-
|
7
|
-
A Bridgetown plugin to [fill in the blank]…
|
8
6
|
|
9
7
|
## Installation
|
10
8
|
|
11
|
-
Run this command to add this plugin to your site's Gemfile:
|
9
|
+
1. Run this command to add this plugin to your site's Gemfile:
|
12
10
|
|
13
11
|
```shell
|
14
|
-
$ bundle add
|
12
|
+
$ bundle add bridgetown_notion
|
15
13
|
```
|
16
14
|
|
17
|
-
|
15
|
+
2. Add the following to your site's `config/initializers.rb`:
|
18
16
|
|
19
|
-
```
|
20
|
-
|
17
|
+
```ruby
|
18
|
+
Bridgetown.configure do |config|
|
19
|
+
init :bridgetown_notion do
|
20
|
+
notion_key '<your-notion-connection-key>'
|
21
|
+
notion_db_id '<your-notion-db-id>'
|
22
|
+
end
|
23
|
+
end
|
21
24
|
```
|
22
25
|
|
26
|
+
You should consider using environment variables to store the `notion_key` and `notion_db_id`.
|
27
|
+
It is generally a bad practice to store credentials in version control.
|
28
|
+
|
23
29
|
## Usage
|
24
30
|
|
25
|
-
|
31
|
+
### Notion Setup
|
32
|
+
|
33
|
+
[bridgetown_notion](https://github.com/fbuys/bridgetown_notion) assumes that you have a Notion
|
34
|
+
database table with the following fileds:
|
26
35
|
|
27
|
-
|
36
|
+
| Field | Type |
|
37
|
+
|-|-|
|
38
|
+
| title (required | Title |
|
39
|
+
| published_at (required) | Date |
|
40
|
+
| published | Checkbox |
|
41
|
+
| categories | Multi-selet |
|
42
|
+
| tags | Multi-selet |
|
43
|
+
| author | Select |
|
28
44
|
|
29
|
-
|
45
|
+
Note the case of the fields as this is important.
|
30
46
|
|
31
|
-
|
47
|
+
You would also need to create a Notion connection that provides read access to the specific
|
48
|
+
database. You can find the [instructions for adding a connection over here](https://www.notion.so/help/add-and-manage-connections-with-the-api).
|
32
49
|
|
33
50
|
## Testing
|
34
51
|
|
data/bridgetown_notion.gemspec
CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
|
20
20
|
spec.add_dependency "bridgetown", ">= 1.0", "< 2.0"
|
21
21
|
spec.add_dependency "notion-ruby-client", ">= 1.0", "< 2.0"
|
22
|
+
spec.add_dependency "activesupport"
|
22
23
|
|
23
24
|
spec.add_development_dependency "bundler"
|
24
25
|
spec.add_development_dependency "rake", ">= 13.0"
|
@@ -1,78 +1,21 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "bridgetown_notion/notion_posts_factory"
|
4
|
+
|
3
5
|
module BridgetownNotion
|
4
6
|
class Builder < Bridgetown::Builder
|
5
|
-
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/PerceivedComplexity, Metrics/MethodLength
|
6
7
|
def build
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
title = post.dig("properties", "title", "title", 0, "text", "content")
|
16
|
-
categories = (post.dig("properties", "categories", "multi_select") || [])
|
17
|
-
.map { |c| c["name"] }.join(" ")
|
18
|
-
tags = (post.dig("properties", "tags", "multi_select") || [])
|
19
|
-
.map { |t| t["name"] }.join(" ")
|
20
|
-
is_published = post["properties"]["published"]["checkbox"]
|
21
|
-
published_at = post.dig("properties", "published_at", "date", "start")
|
22
|
-
content = ""
|
23
|
-
|
24
|
-
next if published_at.nil? || title.nil?
|
25
|
-
|
26
|
-
client.block_children(block_id: post.id) do |blocks_page|
|
27
|
-
blocks_page.results.each do |block|
|
28
|
-
content += extract_content(block)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
add_resource :posts, "#{published_at}-#{title}.md" do
|
33
|
-
categories categories
|
34
|
-
content content
|
35
|
-
layout :post
|
36
|
-
published is_published
|
37
|
-
tags tags
|
38
|
-
title title
|
39
|
-
end
|
8
|
+
BridgetownNotion::NotionPostsFactory.create_posts(config.bridgetown_notion).each do |post|
|
9
|
+
add_resource :posts, "#{post[:slug]}.md" do
|
10
|
+
categories post[:categories]
|
11
|
+
content post[:content]
|
12
|
+
layout :post
|
13
|
+
published post[:is_published]
|
14
|
+
tags post[:tags]
|
15
|
+
title post[:title]
|
40
16
|
end
|
41
17
|
end
|
42
18
|
end
|
43
|
-
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/PerceivedComplexity, Metrics/MethodLength
|
44
|
-
|
45
|
-
private
|
46
|
-
|
47
|
-
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
48
|
-
def extract_content(block)
|
49
|
-
result = block.dig("heading_1", "rich_text", 0, "text", "content")
|
50
|
-
return "# #{result}\n" if result
|
51
|
-
|
52
|
-
result = block.dig("heading_2", "rich_text", 0, "text", "content")
|
53
|
-
return "## #{result}\n" if result
|
54
|
-
|
55
|
-
result = block.dig("heading_3", "rich_text", 0, "text", "content")
|
56
|
-
return "### #{result}\n" if result
|
57
|
-
|
58
|
-
result = block.dig("heading_4", "rich_text", 0, "text", "content")
|
59
|
-
return "#### #{result}\n" if result
|
60
|
-
|
61
|
-
result = block.dig("heading_5", "rich_text", 0, "text", "content")
|
62
|
-
return "##### #{result}\n" if result
|
63
|
-
|
64
|
-
result = block.dig("heading_6", "rich_text", 0, "text", "content")
|
65
|
-
return "###### #{result}\n" if result
|
66
|
-
|
67
|
-
result = block.dig("paragraph", "rich_text", 0, "text", "content")
|
68
|
-
return "#{result}\n" if result
|
69
|
-
|
70
|
-
result = block.dig("bulleted_list_item", "rich_text", 0, "text", "content")
|
71
|
-
return "- #{result}\n" if result
|
72
|
-
|
73
|
-
"\n"
|
74
|
-
end
|
75
|
-
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
76
19
|
end
|
77
20
|
end
|
78
21
|
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BridgetownNotion
|
4
|
+
class NotionPostsFactory
|
5
|
+
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
6
|
+
def self.create_posts(config)
|
7
|
+
notion_key = config.notion_key
|
8
|
+
notion_db_id = config.notion_db_id
|
9
|
+
|
10
|
+
Notion.configure do |notion|
|
11
|
+
notion.token = notion_key
|
12
|
+
end
|
13
|
+
|
14
|
+
notion_client = Notion::Client.new
|
15
|
+
|
16
|
+
result = []
|
17
|
+
|
18
|
+
notion_client.database_query(database_id: notion_db_id) do |posts_page|
|
19
|
+
posts_page.results.each do |post|
|
20
|
+
notion_client.block_children(block_id: post.id) do |blocks_page|
|
21
|
+
post["blocks"] = [*post["blocks"], *blocks_page.results]
|
22
|
+
end
|
23
|
+
|
24
|
+
parsed_post = BridgetownNotion::Parsers::PostParser.parse(post)
|
25
|
+
|
26
|
+
next if parsed_post.published_at.nil? || parsed_post.title.nil?
|
27
|
+
|
28
|
+
result.push({
|
29
|
+
categories: parsed_post.categories,
|
30
|
+
content: parsed_post.content,
|
31
|
+
is_published: parsed_post.is_published,
|
32
|
+
slug: parsed_post.slug,
|
33
|
+
tags: parsed_post.tags,
|
34
|
+
title: parsed_post.title,
|
35
|
+
})
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
result
|
40
|
+
end
|
41
|
+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BridgetownNotion
|
4
|
+
module Parsers
|
5
|
+
class BlockParser
|
6
|
+
def self.parse(block)
|
7
|
+
for_each_block_type do |block_type|
|
8
|
+
content = block.dig(block_type, "rich_text", 0, "text", "content")
|
9
|
+
return [block_type, content] if content
|
10
|
+
end
|
11
|
+
[]
|
12
|
+
end
|
13
|
+
|
14
|
+
private_class_method def self.for_each_block_type
|
15
|
+
pattern = %r{(?<block_type>\w+)(?!.*/)_markdown_generator.rb}
|
16
|
+
markdown_generator_files = Dir[
|
17
|
+
"#{File.dirname(__FILE__)}/../markdown_generators/**/*_markdown_generator.rb"
|
18
|
+
]
|
19
|
+
markdown_generator_files.each do |file|
|
20
|
+
yield file.match(pattern)[:block_type].to_s
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BridgetownNotion
|
4
|
+
module Parsers
|
5
|
+
class CategoriesParser
|
6
|
+
def self.parse(post)
|
7
|
+
categories = (post.dig("properties", "categories", "multi_select") || [])
|
8
|
+
categories.map { |c| c["name"] }.join(" ")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/string/inflections"
|
4
|
+
|
5
|
+
module BridgetownNotion
|
6
|
+
module Parsers
|
7
|
+
class ContentParser
|
8
|
+
def self.parse(post)
|
9
|
+
content = ""
|
10
|
+
blocks = post.fetch("blocks", [])
|
11
|
+
blocks.each do |block|
|
12
|
+
content += get_content(block)
|
13
|
+
end
|
14
|
+
content
|
15
|
+
end
|
16
|
+
|
17
|
+
private_class_method def self.get_content(block)
|
18
|
+
block_type, content = BridgetownNotion::Parsers::BlockParser.parse(block)
|
19
|
+
Object.const_get(
|
20
|
+
"BridgetownNotion::MarkdownGenerators::#{block_type.camelize}MarkdownGenerator"
|
21
|
+
).generate(content)
|
22
|
+
rescue StandardError
|
23
|
+
"\n"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/string/inflections"
|
4
|
+
|
5
|
+
module BridgetownNotion
|
6
|
+
module Parsers
|
7
|
+
class PostParser
|
8
|
+
def initialize(post)
|
9
|
+
@post = post
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.parse(post)
|
13
|
+
new(post)
|
14
|
+
end
|
15
|
+
|
16
|
+
def method_missing(method_name)
|
17
|
+
parser = method_name.to_s.camelize
|
18
|
+
Object.const_get("BridgetownNotion::Parsers::#{parser}Parser").parse(@post)
|
19
|
+
end
|
20
|
+
|
21
|
+
def respond_to_missing?(method_name)
|
22
|
+
Dir["./**/*_parser.rb"].each do |f|
|
23
|
+
return true if f.include?(method_name)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BridgetownNotion
|
4
|
+
module Parsers
|
5
|
+
class SlugParser
|
6
|
+
def self.parse(post)
|
7
|
+
published_at = BridgetownNotion::Parsers::PublishedAtParser.parse(post)
|
8
|
+
title = BridgetownNotion::Parsers::TitleParser.parse(post)
|
9
|
+
"#{published_at}-#{title}".downcase.strip.tr(" ", "-").gsub(%r/[^\w-]/, "")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/bridgetown_notion.rb
CHANGED
@@ -3,3 +3,15 @@
|
|
3
3
|
require "bridgetown"
|
4
4
|
require "bridgetown_notion/builder"
|
5
5
|
require "notion-ruby-client"
|
6
|
+
require "zeitwerk"
|
7
|
+
|
8
|
+
loader = Zeitwerk::Loader.for_gem
|
9
|
+
loader.setup
|
10
|
+
|
11
|
+
Bridgetown.initializer :bridgetown_notion do |config, notion_key:, notion_db_id:|
|
12
|
+
config.bridgetown_notion ||= {}
|
13
|
+
config.bridgetown_notion.notion_key ||= notion_key || ENV.fetch("NOTION_KEY")
|
14
|
+
config.bridgetown_notion.notion_db_id ||= notion_db_id || ENV.fetch("NOTION_DB_ID")
|
15
|
+
|
16
|
+
config.builder BridgetownNotion::Builder
|
17
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bridgetown_notion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Francois Buys
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-03-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bridgetown
|
@@ -50,6 +50,20 @@ dependencies:
|
|
50
50
|
- - "<"
|
51
51
|
- !ruby/object:Gem::Version
|
52
52
|
version: '2.0'
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: activesupport
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
type: :runtime
|
61
|
+
prerelease: false
|
62
|
+
version_requirements: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
53
67
|
- !ruby/object:Gem::Dependency
|
54
68
|
name: bundler
|
55
69
|
requirement: !ruby/object:Gem::Requirement
|
@@ -109,6 +123,24 @@ files:
|
|
109
123
|
- bridgetown_notion.gemspec
|
110
124
|
- lib/bridgetown_notion.rb
|
111
125
|
- lib/bridgetown_notion/builder.rb
|
126
|
+
- lib/bridgetown_notion/markdown_generators/bulleted_list_item_markdown_generator.rb
|
127
|
+
- lib/bridgetown_notion/markdown_generators/heading_1_markdown_generator.rb
|
128
|
+
- lib/bridgetown_notion/markdown_generators/heading_2_markdown_generator.rb
|
129
|
+
- lib/bridgetown_notion/markdown_generators/heading_3_markdown_generator.rb
|
130
|
+
- lib/bridgetown_notion/markdown_generators/heading_4_markdown_generator.rb
|
131
|
+
- lib/bridgetown_notion/markdown_generators/heading_5_markdown_generator.rb
|
132
|
+
- lib/bridgetown_notion/markdown_generators/heading_6_markdown_generator.rb
|
133
|
+
- lib/bridgetown_notion/markdown_generators/paragraph_markdown_generator.rb
|
134
|
+
- lib/bridgetown_notion/notion_posts_factory.rb
|
135
|
+
- lib/bridgetown_notion/parsers/block_parser.rb
|
136
|
+
- lib/bridgetown_notion/parsers/categories_parser.rb
|
137
|
+
- lib/bridgetown_notion/parsers/content_parser.rb
|
138
|
+
- lib/bridgetown_notion/parsers/is_published_parser.rb
|
139
|
+
- lib/bridgetown_notion/parsers/post_parser.rb
|
140
|
+
- lib/bridgetown_notion/parsers/published_at_parser.rb
|
141
|
+
- lib/bridgetown_notion/parsers/slug_parser.rb
|
142
|
+
- lib/bridgetown_notion/parsers/tags_parser.rb
|
143
|
+
- lib/bridgetown_notion/parsers/title_parser.rb
|
112
144
|
- lib/bridgetown_notion/version.rb
|
113
145
|
homepage: https://github.com/fbuys/bridgetown_notion
|
114
146
|
licenses:
|