obsidian-parser 0.5.2 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4b8337e7963b9236dcf2e06bcdbe81c44002546ca23b5c30451c3b9b218c7da4
4
- data.tar.gz: a69df6022fe82cfd016c56a1445c1b5fb23acca02ecd0521c6b455df58df3679
3
+ metadata.gz: 745a2099d25636971d45a662902f56bd3a66289074dad5b3386a617487d79237
4
+ data.tar.gz: 969f3bbd5d98f78d065ae02896f326b1590f27ed04769dcc965f05aaba871ce4
5
5
  SHA512:
6
- metadata.gz: 2b898a0bfabe2aea32d357452b513078bb768be34f5607f07d1c4cbe05d5f9513b8a7c0cdc978995706621964cea01adbe88faf3213c4ff53bb2512b9f42927c
7
- data.tar.gz: 63697c74da15ee1b4c5897706fdaf6e0ed4b87cea7dfcf1ecc97d2e9c0317d06ed8f6ed821debf6ea23a5f2ca33ccbee3e02271f2328f67c68f121cc86d9292a
6
+ metadata.gz: 0f64c3105d81bc757680c24fd0d16ea748116b9bb7b8e45d32f62931b119bca04dc886a127548fe59baff3e00f3367e8d1d466d04c8911dd82e6345015fbabef
7
+ data.tar.gz: be11496450bd3783d91f761c782a273b2a64ddaf54f74ca423cf7999da46b565b9b2da2b9716b02897f4bf19bd056a0d90d0e17449105f2bf375d9dc8fee8337
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.5.3] - 2023-08-01
4
+ - Support non-fully qualified titles when parsing wikilink syntax.
5
+ - Autolink raw URLs.
6
+
3
7
  ## [0.5.2] - 2023-07-30
4
8
  - Fix handling of `index.md` at the root level.
5
9
 
data/Gemfile CHANGED
@@ -10,3 +10,5 @@ gem "rake", "~> 13.0"
10
10
  gem "rspec", "~> 3.0"
11
11
 
12
12
  gem "standard", "~> 1.3"
13
+
14
+ gem "pry"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- obsidian-parser (0.5.2)
4
+ obsidian-parser (0.5.3)
5
5
  kramdown (~> 2.4)
6
6
  kramdown-parser-gfm (~> 1.1)
7
7
 
@@ -9,6 +9,7 @@ GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
11
  ast (2.4.2)
12
+ coderay (1.1.3)
12
13
  diff-lcs (1.5.0)
13
14
  json (2.6.3)
14
15
  kramdown (2.4.0)
@@ -17,10 +18,14 @@ GEM
17
18
  kramdown (~> 2.0)
18
19
  language_server-protocol (3.17.0.3)
19
20
  lint_roller (1.1.0)
21
+ method_source (1.0.0)
20
22
  parallel (1.23.0)
21
23
  parser (3.2.2.3)
22
24
  ast (~> 2.4.1)
23
25
  racc
26
+ pry (0.14.2)
27
+ coderay (~> 1.1)
28
+ method_source (~> 1.0)
24
29
  racc (1.7.1)
25
30
  rainbow (3.1.1)
26
31
  rake (13.0.6)
@@ -74,6 +79,7 @@ PLATFORMS
74
79
 
75
80
  DEPENDENCIES
76
81
  obsidian-parser!
82
+ pry
77
83
  rake (~> 13.0)
78
84
  rspec (~> 3.0)
79
85
  standard (~> 1.3)
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Obsidian
4
+ class MarkdownContent
5
+ def initialize(path, root)
6
+ @path = path
7
+ @root = root
8
+ end
9
+
10
+ def generate_html
11
+ markdown = @path.read
12
+ Obsidian::ObsidianFlavoredMarkdown.parse(markdown, root: @root).to_html
13
+ end
14
+ end
15
+ end
@@ -17,23 +17,54 @@ module Obsidian
17
17
  \]\]
18
18
  }x
19
19
 
20
+ # Match URLs
21
+ # Pattern based on https://gist.github.com/dperini/729294
22
+ LONE_URL = %r{
23
+ (?<!\S) # not preceeded by a non-whitespace character
24
+ https?:// # Protocol
25
+ (?:\S+(?::\S*)?@)? # basic auth
26
+ (?![-_]) # Not followed by a dash or underscore
27
+ (?:[-\w\u00a1-\uffff]{0,63}[^-_]\.)+ # host and domain names
28
+ (?:[a-z\u00a1-\uffff]{2,}\.?) # top level domain
29
+ (?::\\d{2,5})? # Port number
30
+ (?:[/?#]\\S*)? # Resource path
31
+ (?!\S) # not followed by a non-whitespace character
32
+ }x
33
+
34
+ # Workaround for lack of auto-linking in Kramdown.
35
+ # Note: this breaks for URLs included in code blocks.
36
+ def self.auto_link(markdown_text)
37
+ markdown_text.gsub(LONE_URL) do |s|
38
+ "<#{s}>" # Kramdown-specific markup
39
+ end
40
+ end
41
+
20
42
  # Convert Obsidian-flavored-markdown syntax to something parseable
21
43
  # (i.e. with Github-flavored-markdown syntax)
22
- def self.normalize(markdown_text)
44
+ def self.expand_wikilinks(markdown_text, root:)
23
45
  markdown_text.gsub(WIKILINK_SYNTAX) do |s|
24
46
  text = $~[:text]
25
47
  target = $~[:target]
26
48
  fragment = $~[:fragment]
27
- display_text = text.nil? ? target.split("/").last : text
28
- href = fragment.nil? ? target : "#{target}##{fragment}"
49
+ page = root.find_in_tree(target)
50
+ return text.nil? ? target.split("/").last : text if page.nil?
51
+
52
+ display_text = text.nil? ? page.slug.split("/").last : text
53
+ href = fragment.nil? ? page.slug : "#{page.slug}##{fragment}"
29
54
 
30
55
  "[#{display_text}](#{href})"
31
56
  end
32
57
  end
33
58
 
34
- # Parse links from obsidian-flavored-markdown text
35
- def self.parse(markdown_text)
36
- document = Kramdown::Document.new(normalize(markdown_text), input: "GFM")
59
+ def self.normalize(markdown_text, root: nil)
60
+ auto_linked = auto_link(markdown_text)
61
+ return auto_linked if root.nil?
62
+
63
+ expand_wikilinks(auto_link(markdown_text), root: root)
64
+ end
65
+
66
+ def self.parse(markdown_text, root: nil)
67
+ document = Kramdown::Document.new(normalize(markdown_text, root: root), input: "GFM")
37
68
  Obsidian::ParsedMarkdownDocument.new(document)
38
69
  end
39
70
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Obsidian
4
4
  class Parser
5
- VERSION = "0.5.2"
5
+ VERSION = "0.5.3"
6
6
  end
7
7
  end
@@ -4,6 +4,7 @@ require_relative "parser/version"
4
4
  require_relative "parser/parsed_markdown_document"
5
5
  require_relative "parser/obsidian_flavored_markdown"
6
6
  require_relative "parser/page"
7
+ require_relative "parser/markdown_content"
7
8
 
8
9
  require "forwardable"
9
10
 
@@ -14,17 +15,6 @@ module Obsidian
14
15
  (parent_slug == "") ? title : "#{parent_slug}/#{title}"
15
16
  end
16
17
 
17
- class MarkdownContent
18
- def initialize(path)
19
- @path = path
20
- end
21
-
22
- def generate_html
23
- markdown = @path.read
24
- Obsidian::ObsidianFlavoredMarkdown.parse(markdown).to_html
25
- end
26
- end
27
-
28
18
  class Parser
29
19
  attr_reader :index
30
20
 
@@ -49,7 +39,7 @@ module Obsidian
49
39
  @index.add_page(
50
40
  slug,
51
41
  last_modified: path.mtime,
52
- content: MarkdownContent.new(path)
42
+ content: MarkdownContent.new(path, @index)
53
43
  )
54
44
  end
55
45
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: obsidian-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mat Moore
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-30 00:00:00.000000000 Z
11
+ date: 2023-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kramdown
@@ -56,6 +56,7 @@ files:
56
56
  - README.md
57
57
  - Rakefile
58
58
  - lib/obsidian/parser.rb
59
+ - lib/obsidian/parser/markdown_content.rb
59
60
  - lib/obsidian/parser/obsidian_flavored_markdown.rb
60
61
  - lib/obsidian/parser/page.rb
61
62
  - lib/obsidian/parser/parsed_markdown_document.rb