Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AllCops:
TargetRubyVersion: 2.7
TargetRubyVersion: 3.3

Style/StringLiterals:
Enabled: true
Expand Down
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.7
3.3.6
5 changes: 3 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
md_to_notion (0.1.3)
md_to_notion (0.1.5)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -51,6 +51,7 @@ GEM

PLATFORMS
arm64-darwin-20
arm64-darwin-23
x86_64-linux

DEPENDENCIES
Expand All @@ -61,4 +62,4 @@ DEPENDENCIES
rubocop (~> 1.21)

BUNDLED WITH
2.3.26
2.6.6
6 changes: 3 additions & 3 deletions lib/md_to_notion/blocks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def self.file(url, type:)
}
end

def self.rich_text(token, annotations: {}, href: nil, link: nil)
def self.rich_text(token, annotations: {})
default_annotations = {
bold: token[:type] == :bold,
italic: token[:type] == :italic,
Expand All @@ -112,10 +112,10 @@ def self.rich_text(token, annotations: {}, href: nil, link: nil)
type: "text",
text: {
content: token[:text],
link: link
link: token[:link]
},
annotations: default_annotations.merge(annotations),
href: href
href: token[:link]
}
end
end
Expand Down
11 changes: 0 additions & 11 deletions lib/md_to_notion/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ def tokenize
next @stack << " "
elsif next_char == "#"
tokenize_heading
elsif next_char == "["
tokenize_link
elsif next_char == "!"
tokenize_image
elsif ALLOWED_VIDEO_EMBED_URLS.join("").include?(peek(41))
Expand Down Expand Up @@ -99,15 +97,6 @@ def tokenize_bullet_list
@index += ::Regexp.last_match(0).length
end

def tokenize_link
line = @markdown[@index..].split("\n").first
raise InvalidTokenSyntaxError, "Invalid link syntax: #{line}" \
unless line =~ LINK

@tokens << link(::Regexp.last_match(0))
@index += ::Regexp.last_match(0).length
end

def tokenize_image
line = @markdown[@index..].split("\n").first
raise InvalidTokenSyntaxError, "Invalid image syntax: #{line}" \
Expand Down
22 changes: 15 additions & 7 deletions lib/md_to_notion/tokens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Tokens
QUOTE = /^> (.+)/.freeze
GH_EMBED_FILE = %r{https://user-images\.githubusercontent\.com/.+\.[a-zA-Z]+}.freeze
EMBED_FILE_REGEXES = [GH_EMBED_FILE].freeze
LINK = /\[([^\]]+)\]\(([^)]+)\)/.freeze

def heading_1(match)
{ type: :heading_1, rich_texts: tokenize_rich_text(match.gsub(/^# /, "")) }
Expand Down Expand Up @@ -76,7 +77,8 @@ def embeded_file(match)

def tokenize_rich_text(text)
# use a regular expression to capture all the rich text elements and the text between them as separate groups
groups = text.scan(/(`[^`]*`|\*\*[^*]*\*\*|\*[^*]*\*|~~[^~]*~~|[^`*~]+)/).flatten
# use non-capturing groups (?:) instead of capturing groups () for the link parts
groups = text.scan(/(`[^`]*`|\*\*[^*]*\*\*|\*[^*]*\*|~~[^~]*~~|\[(?:[^\]]+)\]\((?:[^)]+)\)|[^`*~\[\]]+)/).flatten

# map the groups to tokens
groups.map do |group|
Expand All @@ -89,6 +91,8 @@ def tokenize_rich_text(text)
italic(group)
when /^~~/
strikethrough(group)
when /^\[/
link(group)
else
text(group)
end
Expand All @@ -99,20 +103,24 @@ def code(text)
{ type: :code, text: text.gsub(/^`/, "").gsub(/`$/, "") }
end

def italic(match)
{ type: :italic, text: match.gsub(/\*/, "") }
end

def bold(match)
{ type: :bold, text: match.gsub(/\*/, "") }
end

def text(match)
{ type: :text, text: match }
def italic(match)
{ type: :italic, text: match.gsub(/\*/, "") }
end

def strikethrough(match)
{ type: :strikethrough, text: match.gsub(/~~/, "") }
end

def link(match)
{ type: :link, text: match.gsub(LINK, '\1'), link: match.gsub(LINK, '\2') }
end

def text(match)
{ type: :text, text: match }
end
Comment on lines +110 to +124
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rearrange to follow the same order as in the case block

end
end
2 changes: 1 addition & 1 deletion lib/md_to_notion/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module MdToNotion
VERSION = "0.1.4"
VERSION = "0.1.5"
end
2 changes: 1 addition & 1 deletion spec/fixtures/test1.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

1. number

paragraph
paragraph with a link to [Notion](https://www.notion.so/)

```c
int main() {
Expand Down
16 changes: 15 additions & 1 deletion spec/fixtures/test1_blocks.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
{
"type": "text",
"text": {
"content": "paragraph",
"content": "paragraph with a link to ",
"link": null
},
"annotations": {
Expand All @@ -150,6 +150,20 @@
"code": false
},
"href": null
}, {
"type": "text",
"text": {
"content": "Notion",
"link": "https://www.notion.so/"
},
"annotations": {
"bold": false,
"italic": false,
"strikethrough": false,
"underline": false,
"code": false
},
"href": "https://www.notion.so/"
}
]
}
Expand Down