File tree 7 files changed +65
-9
lines changed
7 files changed +65
-9
lines changed Original file line number Diff line number Diff line change 1
1
PATH
2
2
remote: .
3
3
specs:
4
- md-to-notion (0.1.0 )
4
+ md_to_notion (0.1.3 )
5
5
6
6
GEM
7
7
remote: https://rubygems.org/
@@ -54,7 +54,7 @@ PLATFORMS
54
54
x86_64-linux
55
55
56
56
DEPENDENCIES
57
- md-to-notion !
57
+ md_to_notion !
58
58
pry
59
59
rake (~> 13.0 )
60
60
rspec (~> 3.0 )
Original file line number Diff line number Diff line change @@ -87,13 +87,13 @@ def self.numbered_list(texts, children: nil)
87
87
block
88
88
end
89
89
90
- def self . image
90
+ def self . file ( url , type : )
91
91
{
92
- type : "image" ,
93
- image : {
92
+ type : type ,
93
+ " #{ type } " : {
94
94
type : "external" ,
95
95
external : {
96
- url : link
96
+ url : url
97
97
}
98
98
}
99
99
}
Original file line number Diff line number Diff line change @@ -6,6 +6,10 @@ module MdToNotion
6
6
class Lexer
7
7
include Tokens
8
8
9
+ ALLOWED_VIDEO_EMBED_URLS = [
10
+ "https://user-images.githubusercontent.com/"
11
+ ]
12
+
9
13
class InvalidTokenSyntaxError < StandardError ; end
10
14
11
15
def initialize ( markdown )
@@ -29,6 +33,8 @@ def tokenize
29
33
tokenize_link
30
34
elsif next_char == "!"
31
35
tokenize_image
36
+ elsif ALLOWED_VIDEO_EMBED_URLS . join ( "" ) . include? ( peek ( 41 ) )
37
+ tokenize_embeded_file
32
38
elsif next_char == ">"
33
39
tokenize_quote
34
40
elsif next_char == "-"
@@ -111,6 +117,22 @@ def tokenize_image
111
117
@index += ::Regexp . last_match ( 0 ) . length
112
118
end
113
119
120
+ def tokenize_embeded_file
121
+ line = @markdown [ @index ..] . split ( "\n " ) . first
122
+ match = nil
123
+ EMBED_FILE_REGEXES . each do |regex |
124
+ if line =~ regex
125
+ match = ::Regexp . last_match ( 0 )
126
+ break
127
+ end
128
+ end
129
+
130
+ raise InvalidTokenSyntaxError , "Invalid embed file syntax: #{ line } " unless match
131
+
132
+ @tokens << embeded_file ( match )
133
+ @index += match . length
134
+ end
135
+
114
136
def tokenize_quote
115
137
line = @markdown [ @index ..] . split ( "\n " ) . first
116
138
raise InvalidTokenSyntaxError , "Invalid quote syntax: #{ line } " \
Original file line number Diff line number Diff line change @@ -33,7 +33,10 @@ def self.ast_to_notion_blocks(ast)
33
33
when :numbered_list
34
34
Block . numbered_list ( token [ :rich_texts ] )
35
35
when :image
36
- Block . image ( token [ :text ] , token [ :rich_texts ] )
36
+ Block . file ( token [ :url ] , type : "image" )
37
+ when :embeded_file
38
+ # @TODO support more file types
39
+ Block . file ( token [ :url ] , type : "video" )
37
40
when :quote
38
41
Block . quote ( token [ :rich_texts ] )
39
42
end
Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ module Tokens
10
10
NUMBERED_LIST = /^([0-9]+)\. (.+)/ . freeze
11
11
IMAGE = /!\[ ([^\] ]+)\] \( ([^)]+)\) / . freeze
12
12
QUOTE = /^> (.+)/ . freeze
13
+ GH_EMBED_FILE = %r{https://user-images\. githubusercontent\. com/.+\. [a-zA-Z]+} . freeze
14
+ EMBED_FILE_REGEXES = [ GH_EMBED_FILE ] . freeze
13
15
14
16
def heading_1 ( match )
15
17
{ type : :heading_1 , rich_texts : tokenize_rich_text ( match . gsub ( /^# / , "" ) ) }
@@ -51,8 +53,7 @@ def numbered_list(match, nesting: 0)
51
53
def image ( match )
52
54
{
53
55
type : :image ,
54
- rich_texts : tokenize_rich_text ( match . gsub ( /!\[ ([^\] ]+)\] \( ([^)]+)\) / , '\1' ) ) ,
55
- link : match . gsub ( /!\[ ([^\] ]+)\] \( ([^)]+)\) / , '\2' )
56
+ url : match . gsub ( /!\[ ([^\] ]+)\] \( ([^)]+)\) / , '\2' )
56
57
}
57
58
end
58
59
@@ -64,6 +65,13 @@ def quote(match)
64
65
{ type : :quote , rich_texts : tokenize_rich_text ( match . gsub ( /^> / , "" ) ) }
65
66
end
66
67
68
+ def embeded_file ( match )
69
+ {
70
+ type : :embeded_file ,
71
+ url : match
72
+ }
73
+ end
74
+
67
75
## rich text objects
68
76
69
77
def tokenize_rich_text ( text )
Original file line number Diff line number Diff line change @@ -15,3 +15,8 @@ int main() {
15
15
return 0;
16
16
}
17
17
```
18
+
19
+ https://user-images.githubusercontent.com/58893812/211213302-7f38960c-dd35-4955-8115-166f83d9c3ed.mov
20
+
21
+ ![ an image] ( https://image.jpeg )
22
+
Original file line number Diff line number Diff line change 176
176
],
177
177
"language" : " c"
178
178
}
179
+ },
180
+ {
181
+ "type" : " video" ,
182
+ "video" : {
183
+ "type" : " external" ,
184
+ "external" : {
185
+ "url" : " https://user-images.githubusercontent.com/58893812/211213302-7f38960c-dd35-4955-8115-166f83d9c3ed.mov"
186
+ }
187
+ }
188
+ },
189
+ {
190
+ "type" : " image" ,
191
+ "image" : {
192
+ "type" : " external" ,
193
+ "external" : {
194
+ "url" : " https://image.jpeg"
195
+ }
196
+ }
179
197
}
180
198
]
You can’t perform that action at this time.
0 commit comments