Skip to content

Commit dd5aed6

Browse files
authored
Merge pull request #2 from rieg-ec/feat/files
feat: add support for files and videos
2 parents bbb8ad9 + 7558c89 commit dd5aed6

File tree

7 files changed

+65
-9
lines changed

7 files changed

+65
-9
lines changed

Gemfile.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
md-to-notion (0.1.0)
4+
md_to_notion (0.1.3)
55

66
GEM
77
remote: https://rubygems.org/
@@ -54,7 +54,7 @@ PLATFORMS
5454
x86_64-linux
5555

5656
DEPENDENCIES
57-
md-to-notion!
57+
md_to_notion!
5858
pry
5959
rake (~> 13.0)
6060
rspec (~> 3.0)

lib/md_to_notion/blocks.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ def self.numbered_list(texts, children: nil)
8787
block
8888
end
8989

90-
def self.image
90+
def self.file(url, type:)
9191
{
92-
type: "image",
93-
image: {
92+
type: type,
93+
"#{type}": {
9494
type: "external",
9595
external: {
96-
url: link
96+
url: url
9797
}
9898
}
9999
}

lib/md_to_notion/lexer.rb

+22
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ module MdToNotion
66
class Lexer
77
include Tokens
88

9+
ALLOWED_VIDEO_EMBED_URLS = [
10+
"https://user-images.githubusercontent.com/"
11+
]
12+
913
class InvalidTokenSyntaxError < StandardError; end
1014

1115
def initialize(markdown)
@@ -29,6 +33,8 @@ def tokenize
2933
tokenize_link
3034
elsif next_char == "!"
3135
tokenize_image
36+
elsif ALLOWED_VIDEO_EMBED_URLS.join("").include?(peek(41))
37+
tokenize_embeded_file
3238
elsif next_char == ">"
3339
tokenize_quote
3440
elsif next_char == "-"
@@ -111,6 +117,22 @@ def tokenize_image
111117
@index += ::Regexp.last_match(0).length
112118
end
113119

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+
114136
def tokenize_quote
115137
line = @markdown[@index..].split("\n").first
116138
raise InvalidTokenSyntaxError, "Invalid quote syntax: #{line}" \

lib/md_to_notion/parser.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ def self.ast_to_notion_blocks(ast)
3333
when :numbered_list
3434
Block.numbered_list(token[:rich_texts])
3535
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")
3740
when :quote
3841
Block.quote(token[:rich_texts])
3942
end

lib/md_to_notion/tokens.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ module Tokens
1010
NUMBERED_LIST = /^([0-9]+)\. (.+)/.freeze
1111
IMAGE = /!\[([^\]]+)\]\(([^)]+)\)/.freeze
1212
QUOTE = /^> (.+)/.freeze
13+
GH_EMBED_FILE = %r{https://user-images\.githubusercontent\.com/.+\.[a-zA-Z]+}.freeze
14+
EMBED_FILE_REGEXES = [GH_EMBED_FILE].freeze
1315

1416
def heading_1(match)
1517
{ type: :heading_1, rich_texts: tokenize_rich_text(match.gsub(/^# /, "")) }
@@ -51,8 +53,7 @@ def numbered_list(match, nesting: 0)
5153
def image(match)
5254
{
5355
type: :image,
54-
rich_texts: tokenize_rich_text(match.gsub(/!\[([^\]]+)\]\(([^)]+)\)/, '\1')),
55-
link: match.gsub(/!\[([^\]]+)\]\(([^)]+)\)/, '\2')
56+
url: match.gsub(/!\[([^\]]+)\]\(([^)]+)\)/, '\2')
5657
}
5758
end
5859

@@ -64,6 +65,13 @@ def quote(match)
6465
{ type: :quote, rich_texts: tokenize_rich_text(match.gsub(/^> /, "")) }
6566
end
6667

68+
def embeded_file(match)
69+
{
70+
type: :embeded_file,
71+
url: match
72+
}
73+
end
74+
6775
## rich text objects
6876

6977
def tokenize_rich_text(text)

spec/fixtures/test1.md

+5
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ int main() {
1515
return 0;
1616
}
1717
```
18+
19+
https://user-images.githubusercontent.com/58893812/211213302-7f38960c-dd35-4955-8115-166f83d9c3ed.mov
20+
21+
![an image](https://image.jpeg)
22+

spec/fixtures/test1_blocks.json

+18
Original file line numberDiff line numberDiff line change
@@ -176,5 +176,23 @@
176176
],
177177
"language": "c"
178178
}
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+
}
179197
}
180198
]

0 commit comments

Comments
 (0)