bridgetown-core 0.19.3 → 0.21.0.beta4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. checksums.yaml +4 -4
  2. data/bridgetown-core.gemspec +1 -2
  3. data/lib/bridgetown-core.rb +37 -29
  4. data/lib/bridgetown-core/cleaner.rb +9 -3
  5. data/lib/bridgetown-core/collection.rb +177 -78
  6. data/lib/bridgetown-core/commands/base.rb +9 -0
  7. data/lib/bridgetown-core/commands/build.rb +0 -11
  8. data/lib/bridgetown-core/commands/concerns/git_helpers.rb +20 -0
  9. data/lib/bridgetown-core/commands/configure.rb +8 -3
  10. data/lib/bridgetown-core/commands/console.rb +4 -0
  11. data/lib/bridgetown-core/commands/doctor.rb +1 -19
  12. data/lib/bridgetown-core/commands/new.rb +6 -6
  13. data/lib/bridgetown-core/commands/plugins.rb +14 -13
  14. data/lib/bridgetown-core/commands/serve.rb +0 -14
  15. data/lib/bridgetown-core/commands/webpack.rb +75 -0
  16. data/lib/bridgetown-core/commands/webpack/enable-postcss.rb +12 -0
  17. data/lib/bridgetown-core/commands/webpack/setup.rb +4 -0
  18. data/lib/bridgetown-core/commands/webpack/update.rb +3 -0
  19. data/lib/bridgetown-core/commands/webpack/webpack.config.js +18 -0
  20. data/lib/{site_template/webpack.config.js.erb → bridgetown-core/commands/webpack/webpack.defaults.js.erb} +32 -16
  21. data/lib/bridgetown-core/component.rb +183 -0
  22. data/lib/bridgetown-core/concerns/data_accessible.rb +1 -0
  23. data/lib/bridgetown-core/concerns/front_matter_importer.rb +52 -0
  24. data/lib/bridgetown-core/concerns/layout_placeable.rb +1 -1
  25. data/lib/bridgetown-core/concerns/site/configurable.rb +10 -10
  26. data/lib/bridgetown-core/concerns/site/content.rb +56 -15
  27. data/lib/bridgetown-core/concerns/site/localizable.rb +3 -5
  28. data/lib/bridgetown-core/concerns/site/processable.rb +6 -4
  29. data/lib/bridgetown-core/concerns/site/renderable.rb +26 -0
  30. data/lib/bridgetown-core/concerns/site/writable.rb +12 -2
  31. data/lib/bridgetown-core/concerns/validatable.rb +2 -5
  32. data/lib/bridgetown-core/configuration.rb +61 -33
  33. data/lib/bridgetown-core/configurations/bt-postcss.rb +6 -6
  34. data/lib/bridgetown-core/configurations/netlify.rb +1 -0
  35. data/lib/bridgetown-core/configurations/tailwindcss.rb +6 -6
  36. data/lib/bridgetown-core/converter.rb +23 -0
  37. data/lib/bridgetown-core/converters/erb_templates.rb +51 -35
  38. data/lib/bridgetown-core/converters/identity.rb +0 -9
  39. data/lib/bridgetown-core/converters/liquid_templates.rb +1 -1
  40. data/lib/bridgetown-core/converters/markdown.rb +14 -4
  41. data/lib/bridgetown-core/converters/markdown/kramdown_parser.rb +5 -38
  42. data/lib/bridgetown-core/converters/ruby_templates.rb +17 -0
  43. data/lib/bridgetown-core/converters/smartypants.rb +3 -1
  44. data/lib/bridgetown-core/core_ext/psych.rb +19 -0
  45. data/lib/bridgetown-core/current.rb +10 -0
  46. data/lib/bridgetown-core/document.rb +9 -16
  47. data/lib/bridgetown-core/drops/collection_drop.rb +1 -1
  48. data/lib/bridgetown-core/drops/page_drop.rb +4 -0
  49. data/lib/bridgetown-core/drops/relations_drop.rb +23 -0
  50. data/lib/bridgetown-core/drops/resource_drop.rb +83 -0
  51. data/lib/bridgetown-core/drops/site_drop.rb +33 -8
  52. data/lib/bridgetown-core/drops/unified_payload_drop.rb +5 -0
  53. data/lib/bridgetown-core/entry_filter.rb +17 -28
  54. data/lib/bridgetown-core/errors.rb +0 -2
  55. data/lib/bridgetown-core/filters.rb +3 -26
  56. data/lib/bridgetown-core/filters/from_liquid.rb +23 -0
  57. data/lib/bridgetown-core/filters/url_filters.rb +12 -0
  58. data/lib/bridgetown-core/frontmatter_defaults.rb +1 -1
  59. data/lib/bridgetown-core/generators/prototype_generator.rb +59 -20
  60. data/lib/bridgetown-core/helpers.rb +48 -9
  61. data/lib/bridgetown-core/layout.rb +53 -21
  62. data/lib/bridgetown-core/liquid_renderer/file.rb +1 -0
  63. data/lib/bridgetown-core/liquid_renderer/table.rb +1 -0
  64. data/lib/bridgetown-core/model/base.rb +138 -0
  65. data/lib/bridgetown-core/model/builder_origin.rb +40 -0
  66. data/lib/bridgetown-core/model/origin.rb +38 -0
  67. data/lib/bridgetown-core/model/repo_origin.rb +126 -0
  68. data/lib/bridgetown-core/page.rb +11 -2
  69. data/lib/bridgetown-core/plugin_manager.rb +1 -3
  70. data/lib/bridgetown-core/publisher.rb +8 -2
  71. data/lib/bridgetown-core/reader.rb +37 -22
  72. data/lib/bridgetown-core/readers/data_reader.rb +5 -5
  73. data/lib/bridgetown-core/readers/defaults_reader.rb +1 -1
  74. data/lib/bridgetown-core/readers/layout_reader.rb +1 -1
  75. data/lib/bridgetown-core/readers/page_reader.rb +1 -0
  76. data/lib/bridgetown-core/readers/post_reader.rb +5 -4
  77. data/lib/bridgetown-core/regenerator.rb +9 -2
  78. data/lib/bridgetown-core/related_posts.rb +9 -6
  79. data/lib/bridgetown-core/renderer.rb +6 -13
  80. data/lib/bridgetown-core/resource/base.rb +329 -0
  81. data/lib/bridgetown-core/resource/destination.rb +49 -0
  82. data/lib/bridgetown-core/resource/permalink_processor.rb +179 -0
  83. data/lib/bridgetown-core/resource/relations.rb +132 -0
  84. data/lib/bridgetown-core/resource/taxonomy_term.rb +34 -0
  85. data/lib/bridgetown-core/resource/taxonomy_type.rb +56 -0
  86. data/lib/bridgetown-core/resource/transformer.rb +177 -0
  87. data/lib/bridgetown-core/ruby_template_view.rb +11 -11
  88. data/lib/bridgetown-core/site.rb +13 -6
  89. data/lib/bridgetown-core/static_file.rb +33 -10
  90. data/lib/bridgetown-core/tags/highlight.rb +2 -15
  91. data/lib/bridgetown-core/tags/include.rb +1 -1
  92. data/lib/bridgetown-core/tags/post_url.rb +2 -2
  93. data/lib/bridgetown-core/url.rb +1 -0
  94. data/lib/bridgetown-core/utils.rb +49 -43
  95. data/lib/bridgetown-core/utils/require_gems.rb +60 -0
  96. data/lib/bridgetown-core/utils/ruby_exec.rb +6 -9
  97. data/lib/bridgetown-core/utils/ruby_front_matter.rb +39 -0
  98. data/lib/bridgetown-core/version.rb +2 -2
  99. data/lib/bridgetown-core/watcher.rb +2 -1
  100. data/lib/bridgetown-core/yaml_parser.rb +22 -0
  101. data/lib/site_template/config/.keep +0 -0
  102. data/lib/site_template/package.json.erb +4 -4
  103. data/lib/site_template/plugins/site_builder.rb +1 -1
  104. data/lib/site_template/src/_posts/0000-00-00-welcome-to-bridgetown.md.erb +1 -1
  105. metadata +46 -41
  106. data/lib/bridgetown-core/external.rb +0 -58
  107. data/lib/bridgetown-core/page_without_a_file.rb +0 -17
  108. data/lib/bridgetown-core/path_manager.rb +0 -31
  109. data/lib/bridgetown-core/readers/collection_reader.rb +0 -23
  110. data/lib/bridgetown-core/readers/static_file_reader.rb +0 -25
  111. data/lib/bridgetown-core/utils/exec.rb +0 -26
  112. data/lib/bridgetown-core/utils/internet.rb +0 -37
  113. data/lib/bridgetown-core/utils/platforms.rb +0 -80
  114. data/lib/bridgetown-core/utils/thread_event.rb +0 -31
  115. data/lib/bridgetown-core/utils/win_tz.rb +0 -75
@@ -6,6 +6,7 @@ module Bridgetown
6
6
  mutable true
7
7
 
8
8
  attr_accessor :page, :layout, :content, :paginator
9
+ alias_method :resource, :page
9
10
 
10
11
  def bridgetown
11
12
  BridgetownDrop.global
@@ -15,6 +16,10 @@ module Bridgetown
15
16
  @site_drop ||= SiteDrop.new(@obj)
16
17
  end
17
18
 
19
+ def collections
20
+ @obj.collections
21
+ end
22
+
18
23
  private
19
24
 
20
25
  def fallback_data
@@ -4,12 +4,14 @@ module Bridgetown
4
4
  class EntryFilter
5
5
  attr_reader :site
6
6
  SPECIAL_LEADING_CHAR_REGEX = %r!\A#{Regexp.union([".", "_", "#", "~"])}!o.freeze
7
+ SPECIAL_LEADING_CHAR_NO_UNDERSCORES_REGEX = %r!\A#{Regexp.union([".", "#", "~"])}!o.freeze
7
8
 
8
- def initialize(site, base_directory = nil)
9
+ def initialize(site, base_directory: nil, include_underscores: false)
9
10
  @site = site
10
11
  @base_directory = derive_base_directory(
11
12
  @site, base_directory.to_s.dup
12
13
  )
14
+ @include_underscores = include_underscores
13
15
  end
14
16
 
15
17
  def base_directory
@@ -32,8 +34,6 @@ module Bridgetown
32
34
  # Reject this entry if it is just a "dot" representation.
33
35
  # e.g.: '.', '..', '_movies/.', 'music/..', etc
34
36
  next true if e.end_with?(".")
35
- # Reject this entry if it is a symlink.
36
- next true if symlink?(e)
37
37
  # Do not reject this entry if it is included.
38
38
  next false if included?(e)
39
39
 
@@ -43,13 +43,18 @@ module Bridgetown
43
43
  end
44
44
 
45
45
  def included?(entry)
46
- glob_include?(site.include, entry) ||
47
- glob_include?(site.include, File.basename(entry))
46
+ glob_include?(site.config.include, entry) ||
47
+ glob_include?(site.config.include, File.basename(entry))
48
48
  end
49
49
 
50
50
  def special?(entry)
51
- SPECIAL_LEADING_CHAR_REGEX.match?(entry) ||
52
- SPECIAL_LEADING_CHAR_REGEX.match?(File.basename(entry))
51
+ use_regex = if @include_underscores
52
+ SPECIAL_LEADING_CHAR_NO_UNDERSCORES_REGEX
53
+ else
54
+ SPECIAL_LEADING_CHAR_REGEX
55
+ end
56
+
57
+ use_regex.match?(entry) || use_regex.match?(File.basename(entry))
53
58
  end
54
59
 
55
60
  def backup?(entry)
@@ -57,7 +62,9 @@ module Bridgetown
57
62
  end
58
63
 
59
64
  def excluded?(entry)
60
- glob_include?(site.exclude - site.include, relative_to_source(entry)).tap do |excluded|
65
+ glob_include?(
66
+ site.config.exclude - site.config.include, relative_to_source(entry)
67
+ ).tap do |excluded|
61
68
  if excluded
62
69
  Bridgetown.logger.debug(
63
70
  "EntryFilter:",
@@ -67,33 +74,15 @@ module Bridgetown
67
74
  end
68
75
  end
69
76
 
70
- # --
71
- # TODO: this is for old Safe mode and can be removed.
72
- # --
73
- def symlink?(_entry)
74
- false
75
- end
76
-
77
- # --
78
- # NOTE: Pathutil#in_path? gets the realpath.
79
- # @param [<Anything>] entry the entry you want to validate.
80
- # Check if a path is outside of our given root.
81
- # --
82
- def symlink_outside_site_source?(entry)
83
- !Pathutil.new(entry).in_path?(
84
- site.in_source_dir
85
- )
86
- end
87
-
88
77
  # Check if an entry matches a specific pattern.
89
78
  # Returns true if path matches against any glob pattern, else false.
90
79
  def glob_include?(enumerator, entry)
91
- entry_with_source = PathManager.join(site.source, entry)
80
+ entry_with_source = File.join(site.source, entry)
92
81
 
93
82
  enumerator.any? do |pattern|
94
83
  case pattern
95
84
  when String
96
- pattern_with_source = PathManager.join(site.source, pattern)
85
+ pattern_with_source = File.join(site.source, pattern)
97
86
 
98
87
  File.fnmatch?(pattern_with_source, entry_with_source) ||
99
88
  entry_with_source.start_with?(pattern_with_source)
@@ -4,8 +4,6 @@ module Bridgetown
4
4
  module Errors
5
5
  FatalException = Class.new(::RuntimeError)
6
6
 
7
- InvalidThemeName = Class.new(FatalException)
8
-
9
7
  DropMutationException = Class.new(FatalException)
10
8
  InvalidPermalinkError = Class.new(FatalException)
11
9
  InvalidYAMLFrontMatterError = Class.new(FatalException)
@@ -31,30 +31,6 @@ module Bridgetown
31
31
  ).convert(input.to_s)
32
32
  end
33
33
 
34
- # TODO: This should be removed, there is no Sass converter
35
- # Convert a Sass string into CSS output.
36
- #
37
- # input - The Sass String to convert.
38
- #
39
- # Returns the CSS formatted String.
40
- def sassify(input)
41
- @context.registers[:site].find_converter_instance(
42
- Bridgetown::Converters::Sass
43
- ).convert(input)
44
- end
45
-
46
- # TODO: This should be removed, there is no Scss converter
47
- # Convert a Scss string into CSS output.
48
- #
49
- # input - The Scss String to convert.
50
- #
51
- # Returns the CSS formatted String.
52
- def scssify(input)
53
- @context.registers[:site].find_converter_instance(
54
- Bridgetown::Converters::Scss
55
- ).convert(input)
56
- end
57
-
58
34
  # Slugify a filename or title.
59
35
  #
60
36
  # input - The filename or title to slugify.
@@ -63,6 +39,7 @@ module Bridgetown
63
39
  # Returns the given filename or title as a lowercase URL String.
64
40
  # See Utils.slugify for more detail.
65
41
  def slugify(input, mode = nil)
42
+ mode = @context.registers[:site].config.slugify_mode if mode.nil?
66
43
  Utils.slugify(input, mode: mode)
67
44
  end
68
45
 
@@ -102,7 +79,7 @@ module Bridgetown
102
79
  #
103
80
  # Returns the escaped String.
104
81
  def cgi_escape(input)
105
- CGI.escape(input)
82
+ CGI.escape(input.to_s)
106
83
  end
107
84
 
108
85
  # URI escape a string.
@@ -131,7 +108,7 @@ module Bridgetown
131
108
  script += "function(c){{var j=c.charCodeAt(0);if((j>=33)&&(j<=126)){"
132
109
  script += "return String.fromCharCode(33+((j+ 14)%94));}"
133
110
  script += "else{return String.fromCharCode(j);}}}));</script>"
134
- script
111
+ script.html_safe
135
112
  end
136
113
 
137
114
  # Replace any whitespace in the input string with a single space
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Filters
5
+ module FromLiquid
6
+ extend Liquid::StandardFilters
7
+
8
+ def strip_html(input)
9
+ FromLiquid.strip_html(input)
10
+ end
11
+
12
+ def strip_newlines(input)
13
+ FromLiquid.strip_newlines(input)
14
+ end
15
+
16
+ def newline_to_br(input)
17
+ FromLiquid.newline_to_br(input)
18
+ end
19
+
20
+ # FYI, truncate and truncate words are already provided by ActiveSupport! =)
21
+ end
22
+ end
23
+ end
@@ -3,6 +3,8 @@
3
3
  module Bridgetown
4
4
  module Filters
5
5
  module URLFilters
6
+ extend self
7
+
6
8
  # Produces an absolute URL based on site.url and site.baseurl.
7
9
  #
8
10
  # input - the URL to make absolute.
@@ -35,6 +37,16 @@ module Bridgetown
35
37
  input.sub(%r!/index\.html?$!, "/")
36
38
  end
37
39
 
40
+ # Strips the extension (if present) off a path/URL
41
+ #
42
+ # @param input [Object] value which responds to `to_s`
43
+ # @return [String]
44
+ def strip_extname(input)
45
+ Pathname.new(input.to_s).yield_self do |path|
46
+ path.dirname + path.basename(".*")
47
+ end.to_s
48
+ end
49
+
38
50
  private
39
51
 
40
52
  def compute_absolute_url(input)
@@ -13,7 +13,7 @@ module Bridgetown
13
13
  end
14
14
 
15
15
  def reset
16
- @glob_cache = {} if @glob_cache
16
+ @glob_cache = {}
17
17
  end
18
18
 
19
19
  def update_deprecated_types(set)
@@ -1,11 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Handles Generated Pages
3
4
  Bridgetown::Hooks.register :pages, :post_init, reloadable: false do |page|
4
5
  if page.class != Bridgetown::PrototypePage && page.data["prototype"].is_a?(Hash)
5
6
  Bridgetown::PrototypeGenerator.add_matching_template(page)
6
7
  end
7
8
  end
8
9
 
10
+ # Handles Resources
11
+ Bridgetown::Hooks.register :resources, :post_read, reloadable: false do |resource|
12
+ if resource.data["prototype"].is_a?(Hash)
13
+ Bridgetown::PrototypeGenerator.add_matching_template(resource)
14
+ end
15
+ end
16
+
17
+ # Ensure sites clear out templates before rebuild
18
+ Bridgetown::Hooks.register :site, :after_reset, reloadable: false do |_site|
19
+ Bridgetown::PrototypeGenerator.matching_templates.clear
20
+ end
21
+
9
22
  module Bridgetown
10
23
  class PrototypeGenerator < Generator
11
24
  priority :low
@@ -13,27 +26,29 @@ module Bridgetown
13
26
  # @return [Bridgetown::Site]
14
27
  attr_reader :site
15
28
 
16
- @matching_templates = []
17
-
18
- def self.add_matching_template(template)
19
- @matching_templates << template
29
+ # @return [Set<Bridgetown::Page, Bridgetown::Resource::Base>]
30
+ def self.matching_templates
31
+ @matching_templates ||= Set.new
20
32
  end
21
33
 
22
- class << self
23
- # @return [Array<Page>]
24
- attr_reader :matching_templates
34
+ def self.add_matching_template(template)
35
+ matching_templates << template
25
36
  end
26
37
 
38
+ # @param site [Bridgetown::Site]
27
39
  def generate(site)
28
40
  @site = site
29
- @configured_collection = "posts"
41
+ @configured_collection = "posts" unless site.uses_resource?
42
+ page_list = site.uses_resource? ? site.collections.pages.resources : site.pages
30
43
 
31
44
  prototype_pages = self.class.matching_templates.select do |page|
32
- site.pages.include? page
45
+ page_list.include?(page)
33
46
  end
34
47
 
35
48
  if prototype_pages.length.positive?
36
- site.pages.reject! do |page|
49
+ ensure_pagination_enabled
50
+
51
+ page_list.reject! do |page|
37
52
  prototype_pages.include? page
38
53
  end
39
54
 
@@ -42,15 +57,24 @@ module Bridgetown
42
57
  next if search_term.nil?
43
58
 
44
59
  terms_matching_pages(search_term).each do |term|
45
- generate_new_page_from_prototype(prototype_page, search_term, term).data
60
+ generate_new_page_from_prototype(prototype_page, search_term, term)
46
61
  end
47
62
  end
48
63
  end
49
64
  end
50
65
 
66
+ def ensure_pagination_enabled
67
+ unless @site.config.dig(:pagination, :enabled)
68
+ Bridgetown.logger.warn(
69
+ "Pagination:",
70
+ "Must be enabled for prototype pages to contain matches"
71
+ )
72
+ end
73
+ end
74
+
51
75
  # Check incoming prototype configuration and normalize options.
52
76
  #
53
- # @param prototype_page [PrototypePage]
77
+ # @param prototype_page [Bridgetown::Page, Bridgetown::Resource::Base]
54
78
  #
55
79
  # @return [String, nil]
56
80
  def validate_search_term(prototype_page)
@@ -62,7 +86,12 @@ module Bridgetown
62
86
  @configured_collection = prototype_page.data["prototype"]["collection"]
63
87
  end
64
88
 
65
- return nil unless site.collections[@configured_collection]
89
+ unless site.collections[@configured_collection]
90
+ Bridgetown.logger.warn(
91
+ "No collection specified for prototype page #{prototype_page.relative_path}"
92
+ )
93
+ return nil
94
+ end
66
95
 
67
96
  # Categories and Tags are unique in that singular and plural front matter
68
97
  # can be present for each
@@ -71,7 +100,7 @@ module Bridgetown
71
100
 
72
101
  def generate_new_page_from_prototype(prototype_page, search_term, term)
73
102
  new_page = PrototypePage.new(prototype_page, @configured_collection, search_term, term)
74
- site.pages << new_page
103
+ site.add_generated_page new_page
75
104
  new_page
76
105
  end
77
106
 
@@ -81,16 +110,26 @@ module Bridgetown
81
110
  #
82
111
  # @return [Array<String>]
83
112
  def terms_matching_pages(search_term)
113
+ pages_list = if site.uses_resource?
114
+ site.collections[@configured_collection].resources
115
+ else
116
+ site.collections[@configured_collection].docs
117
+ end
118
+
84
119
  Bridgetown::Paginate::PaginationIndexer.index_documents_by(
85
- site.collections[@configured_collection].docs, search_term
120
+ pages_list, search_term
86
121
  ).keys
87
122
  end
88
123
  end
89
124
 
90
- class PrototypePage < Page
91
- # @return [Page]
125
+ class PrototypePage < GeneratedPage
126
+ # @return [Bridgetown::Page, Bridgetown::Resource::Base]
92
127
  attr_reader :prototyped_page
93
128
 
129
+ # @param prototyped_page [Bridgetown::Page, Bridgetown::Resource::Base]
130
+ # @param collection [Bridgetown::Collection]
131
+ # @param search_term [String]
132
+ # @param term [String]
94
133
  def initialize(prototyped_page, collection, search_term, term)
95
134
  @prototyped_page = prototyped_page
96
135
  @site = prototyped_page.site
@@ -107,7 +146,7 @@ module Bridgetown
107
146
  validate_data! prototyped_page.path
108
147
  validate_permalink! prototyped_page.path
109
148
 
110
- @dir = Pathname.new(prototyped_page.relative_path).dirname.to_s
149
+ @dir = Pathname.new(prototyped_page.relative_path).dirname.to_s.sub(%r{^_pages}, "")
111
150
  @path = site.in_source_dir(@dir, @name)
112
151
 
113
152
  process_prototype_page_data(collection, search_term, term)
@@ -132,7 +171,7 @@ module Bridgetown
132
171
  slugify_term(term)
133
172
  end
134
173
 
135
- # rubocop:disable Metrics/AbcSize
174
+ # rubocop:todo Metrics/AbcSize
136
175
  def process_title_data_placeholder(search_term, term)
137
176
  if prototyped_page.data["prototype"]["data"]
138
177
  if data["title"]&.include?(":prototype-data-label")
@@ -164,7 +203,7 @@ module Bridgetown
164
203
  end
165
204
 
166
205
  def slugify_term(term)
167
- term_slug = Bridgetown::Utils.slugify(term)
206
+ term_slug = Bridgetown::Utils.slugify(term, mode: site.config.slugify_mode)
168
207
  @url = if permalink.is_a?(String)
169
208
  data["permalink"] = data["permalink"].sub(":term", term_slug)
170
209
  else
@@ -4,8 +4,13 @@ module Bridgetown
4
4
  class RubyTemplateView
5
5
  class Helpers
6
6
  include Bridgetown::Filters
7
+ include Bridgetown::Filters::FromLiquid
7
8
 
8
- attr_reader :view, :site
9
+ # @return [Bridgetown::RubyTemplateView]
10
+ attr_reader :view
11
+
12
+ # @return [Bridgetown::Site]
13
+ attr_reader :site
9
14
 
10
15
  Context = Struct.new(:registers)
11
16
 
@@ -28,20 +33,44 @@ module Bridgetown
28
33
  pairs.select { |_key, truthy| truthy }.keys.join(" ")
29
34
  end
30
35
 
36
+ # Convert a Markdown string into HTML output.
37
+ #
38
+ # @param input [String] the Markdown to convert, if no block is passed
39
+ # @return [String]
40
+ def markdownify(input = nil, &block)
41
+ content = Bridgetown::Utils.reindent_for_markdown(
42
+ block.nil? ? input.to_s : view.capture(&block)
43
+ )
44
+ converter = site.find_converter_instance(Bridgetown::Converters::Markdown)
45
+ safe(converter.convert(content).strip)
46
+ end
47
+
31
48
  # This helper will generate the correct permalink URL for the file path.
32
49
  #
33
50
  # @param relative_path [String, Object] source file path, e.g.
34
- # "_posts/2020-10-20-my-post.md", or object that responds to `url`
51
+ # "_posts/2020-10-20-my-post.md", or object that responds to either
52
+ # `url` or `relative_url`
35
53
  # @return [String] the permalink URL for the file
36
- # @raise [ArgumentError] if the file cannot be found
37
54
  def url_for(relative_path)
38
- path_string = !relative_path.is_a?(String) ? relative_path.url : relative_path
55
+ if relative_path.respond_to?(:relative_url)
56
+ return safe(relative_path.relative_url) # new resource engine
57
+ elsif relative_path.respond_to?(:url)
58
+ return safe(relative_url(relative_path.url)) # old legacy engine
59
+ elsif relative_path.start_with?("/", "http")
60
+ return safe(relative_path)
61
+ end
39
62
 
40
- return path_string if path_string.start_with?("/", "http")
63
+ find_relative_url_for_path(relative_path)
64
+ end
65
+ alias_method :link, :url_for
41
66
 
67
+ # @param relative_path [String] source file path, e.g.
68
+ # "_posts/2020-10-20-my-post.md"
69
+ # @raise [ArgumentError] if the file cannot be found
70
+ def find_relative_url_for_path(relative_path)
42
71
  site.each_site_file do |item|
43
- if item.relative_path == path_string || item.relative_path == "/#{path_string}"
44
- return relative_url(item)
72
+ if item.relative_path == relative_path || item.relative_path == "/#{relative_path}"
73
+ return safe(item.respond_to?(:relative_url) ? item.relative_url : relative_url(item))
45
74
  end
46
75
  end
47
76
 
@@ -51,7 +80,6 @@ module Bridgetown
51
80
  Make sure the document exists and the path is correct.
52
81
  MSG
53
82
  end
54
- alias_method :link, :url_for
55
83
 
56
84
  # This helper will generate the correct permalink URL for the file path.
57
85
  #
@@ -69,7 +97,8 @@ module Bridgetown
69
97
  attr = attr.to_s.tr("_", "-")
70
98
  segments << "#{attr}=\"#{Utils.xml_escape(option)}\""
71
99
  end
72
- "<#{segments.join(" ")}>#{text}</a>"
100
+ # TODO: this might leak an XSS string into text, need to check
101
+ safe("<#{segments.join(" ")}>#{text}</a>")
73
102
  end
74
103
 
75
104
  # Forward all arguments to I18n.t method
@@ -79,6 +108,16 @@ module Bridgetown
79
108
  def t(*args)
80
109
  I18n.send :t, *args
81
110
  end
111
+
112
+ # For template contexts where ActiveSupport's output safety is loaded, we
113
+ # can ensure a string has been marked safe
114
+ #
115
+ # @param input [Object]
116
+ # @return [String]
117
+ def safe(input)
118
+ input.to_s.html_safe
119
+ end
120
+ alias_method :raw, :safe
82
121
  end
83
122
  end
84
123
  end