andromeda_cms 0.8.0-x86_64-linux → 0.9.0-x86_64-linux
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 +4 -4
- data/CHANGELOG.md +54 -0
- data/README.md +4 -2
- data/lib/andromeda/components/errors.rb +19 -1
- data/lib/andromeda/components/import_scanner.rb +13 -0
- data/lib/andromeda/components.rb +56 -2
- data/lib/andromeda/frontmatter.rb +42 -6
- data/lib/andromeda/renderer/literal_expression.rb +40 -4
- data/lib/andromeda/renderer.rb +85 -31
- data/lib/andromeda/version.rb +1 -1
- metadata +10 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a5ffeee40dd7583220d7539274b3e6b29efe17bd6ccb78070aa5bb803b7f35cb
|
|
4
|
+
data.tar.gz: d6f78f468e1557294be852a4a833c9ad58d7fdce42d00304d836dbaedb23778a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a10e9dea2a02cce8798a851bcb1a9cb911638ddbbaf0f0ad5a52ea4a041de5d4466cce24ac87ffbc232aae1cbe517df6adf4fb737d5ac709d087daf94fadcb36
|
|
7
|
+
data.tar.gz: 516b60f3c1eb4563b7dc391f0533cc4b0a66b2ab9431bbe2648504fe0bb81a37b6c196c81047b75eb648cb05e524243e4d743c7ac78cbf35347a526d5e553680
|
data/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,58 @@ All notable changes to this project are documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [0.9.0] - 2026-12-02
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- An MDX expression can read a JSON file imported by a default import, as
|
|
13
|
+
Astro allows: after `import data from "./data.json"`, `{data.name}` works
|
|
14
|
+
in body text, in an HTML attribute and in a component prop. The import
|
|
15
|
+
must be a relative path that stays inside the project, and a missing or
|
|
16
|
+
invalid file raises `Andromeda::Components::InvalidDataImportError`.
|
|
17
|
+
A named import from a JSON file is still unsupported.
|
|
18
|
+
- A template literal without a `${...}` substitution is evaluated as a
|
|
19
|
+
string, so `` {`const a = {x:1}`} `` no longer raises.
|
|
20
|
+
|
|
21
|
+
### Changed
|
|
22
|
+
|
|
23
|
+
- An expression in an HTML element's attribute, such as
|
|
24
|
+
`<a href={frontmatter.url}>`, is now evaluated like one in body text:
|
|
25
|
+
literals and `frontmatter.x` give their value, and anything else raises
|
|
26
|
+
`UnsupportedExpressionError`. It used to output the expression's source,
|
|
27
|
+
such as `href="frontmatter.url"`. **Content that relied on this now fails
|
|
28
|
+
to build.**
|
|
29
|
+
- A spread attribute (`{...props}`) on a component or an HTML element now
|
|
30
|
+
raises `UnsupportedExpressionError`. It used to be dropped, so the
|
|
31
|
+
component rendered without those props. **Content that uses one now
|
|
32
|
+
fails to build.**
|
|
33
|
+
|
|
34
|
+
### Fixed
|
|
35
|
+
|
|
36
|
+
- A string expression directly inside `<script>` or `<style>` is output
|
|
37
|
+
without HTML escaping, as Astro does. `<` and `'` used to be escaped,
|
|
38
|
+
which broke the script.
|
|
39
|
+
- `{false}`, `{null}` and `{undefined}` render nothing, as in Astro. They
|
|
40
|
+
used to raise `UnsupportedExpressionError`, and `enabled={false}` on a
|
|
41
|
+
component did too.
|
|
42
|
+
- An array expression renders its elements one after another, as in Astro
|
|
43
|
+
(`{["a", "b"]}` gives `ab`), instead of Ruby's `["a", "b"]`.
|
|
44
|
+
- A heading made of an expression, such as `# {frontmatter.title}`, gets
|
|
45
|
+
its id and `headings` text from the evaluated value. The id used to be
|
|
46
|
+
empty.
|
|
47
|
+
|
|
48
|
+
## [0.8.1] - 2026-11-21
|
|
49
|
+
|
|
50
|
+
### Changed
|
|
51
|
+
|
|
52
|
+
- Supports tomlrb 3.0. The `tomlrb` dependency is now `>= 2.0, < 4`
|
|
53
|
+
instead of the `~> 2.0` pin added in 0.6.3, so applications on Ruby 3.3
|
|
54
|
+
or later can move to tomlrb 3.0; Ruby 3.2 keeps resolving to 2.x, which
|
|
55
|
+
stays supported. Syntax errors in TOML (`+++`) frontmatter still report
|
|
56
|
+
the file, line and column. Under tomlrb 3.0, an error that tomlrb reports
|
|
57
|
+
without a position, such as a duplicate key, points at the start of the
|
|
58
|
+
frontmatter.
|
|
59
|
+
|
|
8
60
|
## [0.8.0] - 2026-11-14
|
|
9
61
|
|
|
10
62
|
### Changed
|
|
@@ -290,6 +342,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
290
342
|
- Generators: `andromeda:install`, `andromeda:collection`,
|
|
291
343
|
`andromeda:component` and `andromeda:import_astro`.
|
|
292
344
|
|
|
345
|
+
[0.9.0]: https://github.com/AndromedaCMS/andromeda_cms/releases/tag/v0.9.0
|
|
346
|
+
[0.8.1]: https://github.com/AndromedaCMS/andromeda_cms/releases/tag/v0.8.1
|
|
293
347
|
[0.8.0]: https://github.com/AndromedaCMS/andromeda_cms/releases/tag/v0.8.0
|
|
294
348
|
[0.7.1]: https://github.com/AndromedaCMS/andromeda_cms/releases/tag/v0.7.1
|
|
295
349
|
[0.7.0]: https://github.com/AndromedaCMS/andromeda_cms/releases/tag/v0.7.0
|
data/README.md
CHANGED
|
@@ -293,8 +293,10 @@ served by the asset pipeline rather than Astro's image service.
|
|
|
293
293
|
|
|
294
294
|
Not supported: Astro's pages and routing, islands and `client:*` directives,
|
|
295
295
|
remark/rehype plugins, and arbitrary JavaScript in MDX expressions (literals,
|
|
296
|
-
`{frontmatter.x}` and comments are
|
|
297
|
-
than a silent drop).
|
|
296
|
+
including template literals without `${}`, `{frontmatter.x}` and comments are
|
|
297
|
+
evaluated; anything else is an error rather than a silent drop). Inside
|
|
298
|
+
`<script>` and `<style>`, an expression's string value is output unescaped, as
|
|
299
|
+
Astro does.
|
|
298
300
|
|
|
299
301
|
## Development
|
|
300
302
|
|
|
@@ -71,7 +71,7 @@ module Andromeda
|
|
|
71
71
|
location = [path, line].compact.join(":")
|
|
72
72
|
super(
|
|
73
73
|
"#{location}: unsupported MDX expression `{#{source}}` -- only literals, " \
|
|
74
|
-
"`frontmatter.x` references, imported images, and comments are supported"
|
|
74
|
+
"`frontmatter.x` references, imported images and JSON, and comments are supported"
|
|
75
75
|
)
|
|
76
76
|
end
|
|
77
77
|
end
|
|
@@ -95,6 +95,24 @@ module Andromeda
|
|
|
95
95
|
end
|
|
96
96
|
end
|
|
97
97
|
|
|
98
|
+
# Raised when an expression reads a JSON import (`import data from
|
|
99
|
+
# './data.json'` then `{data.name}`) that cannot be loaded: the file is
|
|
100
|
+
# missing, lies outside the project, was not imported by a relative
|
|
101
|
+
# path, or is not valid JSON.
|
|
102
|
+
class InvalidDataImportError < Andromeda::Error
|
|
103
|
+
attr_reader :name, :specifier, :path, :line
|
|
104
|
+
|
|
105
|
+
def initialize(name, specifier, reason, path:, line:)
|
|
106
|
+
@name = name
|
|
107
|
+
@specifier = specifier
|
|
108
|
+
@path = path
|
|
109
|
+
@line = line
|
|
110
|
+
|
|
111
|
+
location = [path, line].compact.join(":")
|
|
112
|
+
super("#{location}: data `#{name}` imported from #{specifier.inspect} #{reason}")
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
98
116
|
# Raised when there is no Rails renderer to hand a partial to -- either
|
|
99
117
|
# `ApplicationController` is not defined (Rails is not loaded / not
|
|
100
118
|
# booted yet) and no `view:` was passed explicitly to
|
|
@@ -54,6 +54,19 @@ module Andromeda
|
|
|
54
54
|
imports
|
|
55
55
|
end
|
|
56
56
|
|
|
57
|
+
# @param tree [Hash] an mdast root node.
|
|
58
|
+
# @return [Hash{String => String}] only the default imports
|
|
59
|
+
# (`import data from '...'`), name -> specifier. `scan` cannot tell
|
|
60
|
+
# these apart from named ones, and for a JSON import that matters: a
|
|
61
|
+
# default import binds the whole document, a named one a single key.
|
|
62
|
+
def default_imports(tree)
|
|
63
|
+
imports = {}
|
|
64
|
+
each_esm_source(tree) do |source|
|
|
65
|
+
source.scan(DEFAULT_IMPORT_RE) { |name, _quote, path| imports[name] = path }
|
|
66
|
+
end
|
|
67
|
+
imports
|
|
68
|
+
end
|
|
69
|
+
|
|
57
70
|
# Walks the whole tree (not just `root.children`) so this keeps
|
|
58
71
|
# working even if a future Sätteri version nests `mdxjsEsm` nodes
|
|
59
72
|
# somewhere other than the top level; valid MDX only ever has them at
|
data/lib/andromeda/components.rb
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "json"
|
|
3
4
|
require "pathname"
|
|
4
5
|
|
|
5
6
|
require_relative "components/errors"
|
|
@@ -49,6 +50,8 @@ module Andromeda
|
|
|
49
50
|
@frontmatter = frontmatter || {}
|
|
50
51
|
@view = view || default_view
|
|
51
52
|
@imports = ImportScanner.scan(tree)
|
|
53
|
+
@default_imports = ImportScanner.default_imports(tree)
|
|
54
|
+
@json_imports = {}
|
|
52
55
|
end
|
|
53
56
|
|
|
54
57
|
# @param name [String] the JSX tag name, e.g. `"Callout"` or
|
|
@@ -102,7 +105,7 @@ module Andromeda
|
|
|
102
105
|
# @return [Object] the evaluated value.
|
|
103
106
|
# @raise [UnsupportedExpressionError]
|
|
104
107
|
def evaluate_expression(source, line: nil, column: nil)
|
|
105
|
-
|
|
108
|
+
evaluate_static(source, line: line)
|
|
106
109
|
rescue StaticExpression::UnsupportedExpressionError
|
|
107
110
|
raise UnsupportedExpressionError.new(source, path: @path, line: line, column: column)
|
|
108
111
|
end
|
|
@@ -193,6 +196,13 @@ module Andromeda
|
|
|
193
196
|
# case).
|
|
194
197
|
def build_props(node)
|
|
195
198
|
(node[:attributes] || []).each_with_object({}) do |attribute, props|
|
|
199
|
+
# Spread ({...p}): the spread value only exists in JS, so the props
|
|
200
|
+
# it carries cannot be known. An error rather than silently
|
|
201
|
+
# rendering the component without them.
|
|
202
|
+
if attribute[:type] == "mdxJsxExpressionAttribute"
|
|
203
|
+
position = node_position(node)
|
|
204
|
+
raise UnsupportedExpressionError.new(attribute[:value].to_s, path: @path, line: position[:line], column: position[:column])
|
|
205
|
+
end
|
|
196
206
|
next unless attribute[:type] == "mdxJsxAttribute"
|
|
197
207
|
|
|
198
208
|
key = self.class.underscore(attribute[:name]).to_sym
|
|
@@ -217,12 +227,56 @@ module Andromeda
|
|
|
217
227
|
name = source.to_s.strip
|
|
218
228
|
return imported_image(name, node) if image_import?(name)
|
|
219
229
|
|
|
220
|
-
|
|
230
|
+
evaluate_static(source, line: node_position(node)[:line])
|
|
221
231
|
rescue StaticExpression::UnsupportedExpressionError
|
|
222
232
|
position = node_position(node)
|
|
223
233
|
raise UnsupportedExpressionError.new(source, path: @path, line: position[:line], column: position[:column])
|
|
224
234
|
end
|
|
225
235
|
|
|
236
|
+
JSON_IMPORT_PATH_RE = /\A([A-Za-z_$][\w$]*)((?:\.[A-Za-z_$][\w$]*)*)\z/.freeze
|
|
237
|
+
|
|
238
|
+
# `StaticExpression`, plus `{data}` / `{data.a.b}` where `data` was bound
|
|
239
|
+
# by a default import of a JSON file (`import data from './data.json'`),
|
|
240
|
+
# which Astro resolves to the parsed document.
|
|
241
|
+
def evaluate_static(source, line:)
|
|
242
|
+
match = JSON_IMPORT_PATH_RE.match(source.to_s.strip)
|
|
243
|
+
if match && json_import?(match[1])
|
|
244
|
+
return StaticExpression.dig_frontmatter(imported_json(match[1], line), match[2].split(".").drop(1))
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
StaticExpression.evaluate(source, frontmatter: @frontmatter)
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
def json_import?(name)
|
|
251
|
+
specifier = @default_imports[name]
|
|
252
|
+
!specifier.nil? && File.extname(specifier).casecmp?(".json")
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# Loaded once per file and name. The same checks as an imported image,
|
|
256
|
+
# in the same order: containment before existence, so a crafted path
|
|
257
|
+
# never learns whether something outside the project exists.
|
|
258
|
+
#
|
|
259
|
+
# @return [Object] the parsed JSON document.
|
|
260
|
+
# @raise [InvalidDataImportError]
|
|
261
|
+
def imported_json(name, line)
|
|
262
|
+
@json_imports.fetch(name) do
|
|
263
|
+
specifier = @default_imports[name]
|
|
264
|
+
invalid = ->(reason) { InvalidDataImportError.new(name, specifier, reason, path: @path, line: line) }
|
|
265
|
+
raise invalid.call("must be a relative path (./ or ../)") unless specifier.start_with?("./", "../") && @path
|
|
266
|
+
|
|
267
|
+
source = File.expand_path(specifier, File.dirname(File.expand_path(@path)))
|
|
268
|
+
content_root = @content_root || File.dirname(source)
|
|
269
|
+
raise invalid.call("resolves outside the project") unless Andromeda::Assets.logical_path(source, content_root: content_root)
|
|
270
|
+
raise invalid.call("does not exist") unless File.file?(source)
|
|
271
|
+
|
|
272
|
+
@json_imports[name] = begin
|
|
273
|
+
JSON.parse(File.read(source))
|
|
274
|
+
rescue JSON::ParserError
|
|
275
|
+
raise invalid.call("is not valid JSON")
|
|
276
|
+
end
|
|
277
|
+
end
|
|
278
|
+
end
|
|
279
|
+
|
|
226
280
|
IMAGE_EXTENSIONS = %w[.png .jpg .jpeg .gif .webp .avif .svg].freeze
|
|
227
281
|
IDENTIFIER_RE = /\A[A-Za-z_$][\w$]*\z/.freeze
|
|
228
282
|
|
|
@@ -220,13 +220,25 @@ module Andromeda
|
|
|
220
220
|
end
|
|
221
221
|
private_class_method :parse_yaml
|
|
222
222
|
|
|
223
|
+
# tomlrb 3.x requires Ruby 3.3+, so Ruby 3.2 still resolves to 2.x. The
|
|
224
|
+
# two majors expose error positions differently (see parse_toml_v2 and
|
|
225
|
+
# parse_toml_v3); Tomlrb::Scanner only exists in 2.x.
|
|
223
226
|
def parse_toml(text, path:, content_start_line:)
|
|
224
227
|
return {} if text.strip.empty?
|
|
225
228
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
229
|
+
if defined?(Tomlrb::Scanner)
|
|
230
|
+
parse_toml_v2(text, path: path, content_start_line: content_start_line)
|
|
231
|
+
else
|
|
232
|
+
parse_toml_v3(text, path: path, content_start_line: content_start_line)
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
private_class_method :parse_toml
|
|
236
|
+
|
|
237
|
+
# tomlrb 2.x. Not calling the public Tomlrb.parse: it rescues
|
|
238
|
+
# Racc::ParseError itself and re-raises as Tomlrb::ParseError carrying
|
|
239
|
+
# only a message, which throws away the scanner's position along with it.
|
|
240
|
+
# Driving the scanner/parser directly keeps that position reachable below.
|
|
241
|
+
def parse_toml_v2(text, path:, content_start_line:)
|
|
230
242
|
scanner = Tomlrb::Scanner.new(StringIO.new(text))
|
|
231
243
|
parser = Tomlrb::Parser.new(scanner)
|
|
232
244
|
normalize_toml_dates(parser.parse.output)
|
|
@@ -235,7 +247,31 @@ module Andromeda
|
|
|
235
247
|
raise syntax_error(e.message, path: path, content_start_line: content_start_line,
|
|
236
248
|
relative_line: relative_line, relative_column: relative_column)
|
|
237
249
|
end
|
|
238
|
-
private_class_method :
|
|
250
|
+
private_class_method :parse_toml_v2
|
|
251
|
+
|
|
252
|
+
# tomlrb 3.x appends the position to a syntax error's message, 1-based
|
|
253
|
+
# and counting the column in characters.
|
|
254
|
+
TOMLRB_POSITION = /\s+at line (\d+), column (\d+)\z/
|
|
255
|
+
|
|
256
|
+
# tomlrb 3.x. Its hand-written parser no longer uses Racc (so Racc may not
|
|
257
|
+
# be loaded at all) and reports the position only inside the message.
|
|
258
|
+
# Errors without one -- duplicate keys, invalid UTF-8, impossible dates
|
|
259
|
+
# (ArgumentError) -- point at the start of the frontmatter, as the 2.x
|
|
260
|
+
# path does when it can't find a position.
|
|
261
|
+
def parse_toml_v3(text, path:, content_start_line:)
|
|
262
|
+
normalize_toml_dates(Tomlrb.parse(text))
|
|
263
|
+
rescue Tomlrb::ParseError, ArgumentError => e
|
|
264
|
+
message = e.message
|
|
265
|
+
relative_line = relative_column = 0
|
|
266
|
+
if (match = TOMLRB_POSITION.match(message))
|
|
267
|
+
message = match.pre_match
|
|
268
|
+
relative_line = match[1].to_i - 1
|
|
269
|
+
relative_column = match[2].to_i - 1
|
|
270
|
+
end
|
|
271
|
+
raise syntax_error(message, path: path, content_start_line: content_start_line,
|
|
272
|
+
relative_line: relative_line, relative_column: relative_column)
|
|
273
|
+
end
|
|
274
|
+
private_class_method :parse_toml_v3
|
|
239
275
|
|
|
240
276
|
# tomlrb represents TOML's date-only and offset-less-datetime types with
|
|
241
277
|
# its own Tomlrb::LocalDate/LocalDateTime (only offset datetimes come
|
|
@@ -260,7 +296,7 @@ module Andromeda
|
|
|
260
296
|
end
|
|
261
297
|
private_class_method :normalize_toml_dates
|
|
262
298
|
|
|
263
|
-
# tomlrb's generated (Racc) parser tracks no token-start positions at
|
|
299
|
+
# tomlrb 2.x's generated (Racc) parser tracks no token-start positions at
|
|
264
300
|
# all, only the underlying StringScanner's current byte offset -- which,
|
|
265
301
|
# by the time the parser rejects a token, already sits *after* that
|
|
266
302
|
# token. So the line this resolves to is exact (the offending token is
|
|
@@ -61,13 +61,21 @@ module Andromeda
|
|
|
61
61
|
when "{" then parse_object
|
|
62
62
|
when "[" then parse_array
|
|
63
63
|
when '"', "'" then parse_string
|
|
64
|
+
when "`" then parse_template_literal
|
|
64
65
|
else
|
|
65
|
-
|
|
66
|
+
matched, value = parse_keyword
|
|
67
|
+
return value if matched
|
|
68
|
+
|
|
69
|
+
parse_number || (raise Error, "unexpected #{@s[@i].inspect} at #{@i}")
|
|
66
70
|
end
|
|
67
71
|
end
|
|
68
72
|
|
|
73
|
+
KEYWORDS = { "true" => true, "false" => false, "null" => nil, "undefined" => nil }.freeze
|
|
74
|
+
|
|
75
|
+
# @return [Array(Boolean, Object)] whether a keyword was read, and its
|
|
76
|
+
# value -- a pair, because `false`/`nil` are themselves values.
|
|
69
77
|
def parse_keyword
|
|
70
|
-
|
|
78
|
+
KEYWORDS.each do |word, value|
|
|
71
79
|
next unless @s[@i, word.length] == word
|
|
72
80
|
# Guard against matching a prefix of a longer identifier, e.g.
|
|
73
81
|
# `truest` should not parse as `true` followed by garbage.
|
|
@@ -75,9 +83,9 @@ module Andromeda
|
|
|
75
83
|
next if after && after.match?(IDENTIFIER_CHAR)
|
|
76
84
|
|
|
77
85
|
@i += word.length
|
|
78
|
-
return
|
|
86
|
+
return [true, value]
|
|
79
87
|
end
|
|
80
|
-
nil
|
|
88
|
+
[false, nil]
|
|
81
89
|
end
|
|
82
90
|
|
|
83
91
|
def parse_number
|
|
@@ -113,6 +121,34 @@ module Andromeda
|
|
|
113
121
|
end
|
|
114
122
|
end
|
|
115
123
|
|
|
124
|
+
# A template literal with no `${...}` substitution is just a string
|
|
125
|
+
# (newlines included). One with a substitution needs a JS evaluator,
|
|
126
|
+
# so it is rejected like any other non-literal.
|
|
127
|
+
def parse_template_literal
|
|
128
|
+
@i += 1 # consume '`'
|
|
129
|
+
buf = +""
|
|
130
|
+
while true
|
|
131
|
+
raise Error, "unterminated template literal" if eof?
|
|
132
|
+
|
|
133
|
+
char = @s[@i]
|
|
134
|
+
if char == "`"
|
|
135
|
+
@i += 1
|
|
136
|
+
return buf
|
|
137
|
+
elsif char == "\\"
|
|
138
|
+
@i += 1
|
|
139
|
+
raise Error, "unterminated template literal" if eof?
|
|
140
|
+
|
|
141
|
+
buf << unescape(@s[@i])
|
|
142
|
+
@i += 1
|
|
143
|
+
elsif char == "$" && @s[@i + 1] == "{"
|
|
144
|
+
raise Error, "template substitution at #{@i}"
|
|
145
|
+
else
|
|
146
|
+
buf << char
|
|
147
|
+
@i += 1
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
116
152
|
def unescape(char)
|
|
117
153
|
{ "n" => "\n", "t" => "\t", "r" => "\r", "b" => "\b", "f" => "\f" }.fetch(char, char)
|
|
118
154
|
end
|
data/lib/andromeda/renderer.rb
CHANGED
|
@@ -151,14 +151,20 @@ module Andromeda
|
|
|
151
151
|
"<h#{depth} id=\"#{Renderer.escape_attr(slug)}\">#{render_children(node)}</h#{depth}>\n"
|
|
152
152
|
end
|
|
153
153
|
|
|
154
|
-
# Only `text` nodes
|
|
155
|
-
# contribute characters; other leaf nodes
|
|
156
|
-
# ...) contribute nothing even though
|
|
157
|
-
# parent nodes (`emphasis`, `strong`,
|
|
158
|
-
# text nodes by recursing into
|
|
154
|
+
# Only `text` nodes and MDX expressions (by their evaluated value, as
|
|
155
|
+
# Astro slugs the rendered text) contribute characters; other leaf nodes
|
|
156
|
+
# (`html`, `break`, `inlineCode`, ...) contribute nothing even though
|
|
157
|
+
# they carry a `:value`, and other parent nodes (`emphasis`, `strong`,
|
|
158
|
+
# ...) contribute their descendant text nodes by recursing into
|
|
159
|
+
# `:children`.
|
|
159
160
|
def heading_text(node)
|
|
160
161
|
return node[:value].to_s if node[:type] == "text"
|
|
161
162
|
|
|
163
|
+
if EXPRESSION_TYPES.include?(node[:type])
|
|
164
|
+
value = expression_value(node)
|
|
165
|
+
return value == UNEVALUATED ? "" : stringify_expression_value(value)
|
|
166
|
+
end
|
|
167
|
+
|
|
162
168
|
(node[:children] || []).map { |child| heading_text(child) }.join
|
|
163
169
|
end
|
|
164
170
|
|
|
@@ -317,8 +323,24 @@ module Andromeda
|
|
|
317
323
|
end
|
|
318
324
|
|
|
319
325
|
def render_html_element(node, name)
|
|
320
|
-
attrs = jsx_attributes(node).map { |key, value| html_attribute(key, value) }.join
|
|
321
|
-
"<#{name}#{attrs}>#{
|
|
326
|
+
attrs = jsx_attributes(node, evaluate: true).map { |key, value| html_attribute(key, value) }.join
|
|
327
|
+
"<#{name}#{attrs}>#{render_html_element_children(node, name)}</#{name}>"
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
# Elements whose content is raw text (JS/CSS), not HTML.
|
|
331
|
+
RAW_TEXT_ELEMENTS = %w[script style].freeze
|
|
332
|
+
EXPRESSION_TYPES = %w[mdxFlowExpression mdxTextExpression].freeze
|
|
333
|
+
|
|
334
|
+
# Inside <script>/<style>, a direct child expression's value is emitted
|
|
335
|
+
# unescaped, matching Astro's MDX output -- escaping `<` or `'` there
|
|
336
|
+
# would break the JS/CSS. Only direct children: anything nested (e.g. a
|
|
337
|
+
# `<p>` MDX wrapped around body text) keeps the normal escaping.
|
|
338
|
+
def render_html_element_children(node, name)
|
|
339
|
+
return render_children(node) unless RAW_TEXT_ELEMENTS.include?(name)
|
|
340
|
+
|
|
341
|
+
(node[:children] || []).map do |child|
|
|
342
|
+
EXPRESSION_TYPES.include?(child[:type]) ? render_expression(child, escape: false) : render_node(child)
|
|
343
|
+
end.join
|
|
322
344
|
end
|
|
323
345
|
|
|
324
346
|
def html_attribute(name, value)
|
|
@@ -329,44 +351,73 @@ module Andromeda
|
|
|
329
351
|
end
|
|
330
352
|
end
|
|
331
353
|
|
|
354
|
+
# @param evaluate [Boolean] true for a plain HTML element: an expression
|
|
355
|
+
# that is not a literal goes to the components collaborator's
|
|
356
|
+
# `evaluate_expression` (`frontmatter.x`, else an error), the same as
|
|
357
|
+
# a body expression, and a spread is an error. False for a component
|
|
358
|
+
# tag, whose real props `Andromeda::Components#build_props` computes
|
|
359
|
+
# from the node itself (it also knows imported images); this hash is
|
|
360
|
+
# only what the collaborator's `#render` is handed.
|
|
332
361
|
# @return [Hash{String => Object}] attribute name -> evaluated value.
|
|
333
|
-
#
|
|
362
|
+
# Without `evaluate`, or without a collaborator that evaluates, a
|
|
334
363
|
# non-literal expression's raw source string is used as a fallback
|
|
335
364
|
# (see `LiteralExpression`).
|
|
336
|
-
def jsx_attributes(node)
|
|
365
|
+
def jsx_attributes(node, evaluate: false)
|
|
366
|
+
evaluate &&= @components.respond_to?(:evaluate_expression)
|
|
337
367
|
(node[:attributes] || []).each_with_object({}) do |attribute, out|
|
|
338
368
|
case attribute[:type]
|
|
339
369
|
when "mdxJsxAttribute"
|
|
340
|
-
out[attribute[:name]] = jsx_attribute_value(attribute[:value])
|
|
370
|
+
out[attribute[:name]] = jsx_attribute_value(attribute[:value], node, evaluate)
|
|
341
371
|
when "mdxJsxExpressionAttribute"
|
|
342
|
-
# Spread ({...props}):
|
|
343
|
-
#
|
|
344
|
-
#
|
|
345
|
-
|
|
346
|
-
# renders with its explicit props in the meantime.
|
|
347
|
-
next
|
|
372
|
+
# Spread ({...props}): its value only exists in JS, so it cannot
|
|
373
|
+
# be expanded. `evaluate_expression` raises for it rather than the
|
|
374
|
+
# attributes silently going missing.
|
|
375
|
+
evaluate_with_components(attribute[:value].to_s, node) if evaluate
|
|
348
376
|
end
|
|
349
377
|
end
|
|
350
378
|
end
|
|
351
379
|
|
|
352
|
-
def jsx_attribute_value(value)
|
|
380
|
+
def jsx_attribute_value(value, node, evaluate)
|
|
353
381
|
case value
|
|
354
382
|
when nil then true # bare boolean prop: <Toggle enabled />
|
|
355
383
|
when String then value
|
|
356
384
|
when Hash
|
|
357
|
-
value[:type] == "mdxJsxAttributeValueExpression"
|
|
385
|
+
return value unless value[:type] == "mdxJsxAttributeValueExpression"
|
|
386
|
+
return LiteralExpression.evaluate(value[:value]) unless evaluate
|
|
387
|
+
|
|
388
|
+
literal, matched = literal_expression(value[:value].to_s)
|
|
389
|
+
matched ? literal : evaluate_with_components(value[:value].to_s, node)
|
|
358
390
|
else
|
|
359
391
|
value
|
|
360
392
|
end
|
|
361
393
|
end
|
|
362
394
|
|
|
363
|
-
def
|
|
395
|
+
def evaluate_with_components(source, node)
|
|
396
|
+
position = node[:position] || {}
|
|
397
|
+
start = position[:start] || {}
|
|
398
|
+
@components.evaluate_expression(source, line: start[:line], column: start[:column])
|
|
399
|
+
end
|
|
400
|
+
|
|
401
|
+
# @param escape [Boolean] false only for raw-text elements
|
|
402
|
+
# (<script>/<style>); see `render_html_element_children`.
|
|
403
|
+
def render_expression(node, escape: true)
|
|
404
|
+
value = expression_value(node)
|
|
405
|
+
return expression_output(value, escape) unless value == UNEVALUATED
|
|
406
|
+
|
|
407
|
+
"<!-- andromeda: unevaluated MDX expression: #{Renderer.escape_html(node[:value].to_s)} -->"
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
UNEVALUATED = Object.new.freeze
|
|
411
|
+
|
|
412
|
+
# @return [Object] the expression's value, or `UNEVALUATED` when there
|
|
413
|
+
# is no collaborator to evaluate a non-literal with.
|
|
414
|
+
def expression_value(node)
|
|
364
415
|
value = node[:value].to_s
|
|
365
416
|
trimmed = value.strip
|
|
366
417
|
return "" if trimmed.start_with?("/*") && trimmed.end_with?("*/")
|
|
367
418
|
|
|
368
419
|
literal, matched = literal_expression(value)
|
|
369
|
-
return
|
|
420
|
+
return literal if matched
|
|
370
421
|
|
|
371
422
|
# The `Andromeda::Components` collaborator knows how to evaluate
|
|
372
423
|
# `{frontmatter.x}` and raise a clear error for anything
|
|
@@ -376,22 +427,22 @@ module Andromeda
|
|
|
376
427
|
# (e.g. a test double that only implements `#render`), fall back to
|
|
377
428
|
# a visible marker rather than raising, so plain
|
|
378
429
|
# Markdown/MDX rendering still works without wiring one up.
|
|
379
|
-
if @components.respond_to?(:evaluate_expression)
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
return Renderer.escape_html(stringify_expression_value(evaluated))
|
|
384
|
-
end
|
|
430
|
+
return evaluate_with_components(value, node) if @components.respond_to?(:evaluate_expression)
|
|
431
|
+
|
|
432
|
+
UNEVALUATED
|
|
433
|
+
end
|
|
385
434
|
|
|
386
|
-
|
|
435
|
+
def expression_output(value, escape)
|
|
436
|
+
text = stringify_expression_value(value)
|
|
437
|
+
escape ? Renderer.escape_html(text) : text
|
|
387
438
|
end
|
|
388
439
|
|
|
389
440
|
# Tries the same literal grammar `LiteralExpression` uses, but reports
|
|
390
441
|
# whether the whole source parsed as a literal instead of silently
|
|
391
442
|
# handing back the original string on failure -- unlike
|
|
392
|
-
# `LiteralExpression.evaluate` (kept as-is for attribute
|
|
393
|
-
# existing callers/tests rely on "fall back to raw source"
|
|
394
|
-
# raising or marking).
|
|
443
|
+
# `LiteralExpression.evaluate` (kept as-is for component attribute
|
|
444
|
+
# values, whose existing callers/tests rely on "fall back to raw source"
|
|
445
|
+
# rather than raising or marking).
|
|
395
446
|
def literal_expression(source)
|
|
396
447
|
parser = LiteralExpression::Parser.new(source)
|
|
397
448
|
value = parser.parse_value
|
|
@@ -401,9 +452,12 @@ module Andromeda
|
|
|
401
452
|
[nil, false]
|
|
402
453
|
end
|
|
403
454
|
|
|
455
|
+
# As JSX renders a child: `null`/`undefined`/`false` render nothing,
|
|
456
|
+
# and an array renders its elements one after another.
|
|
404
457
|
def stringify_expression_value(value)
|
|
405
458
|
case value
|
|
406
|
-
when nil then ""
|
|
459
|
+
when nil, false then ""
|
|
460
|
+
when Array then value.map { |element| stringify_expression_value(element) }.join
|
|
407
461
|
else value.to_s
|
|
408
462
|
end
|
|
409
463
|
end
|
data/lib/andromeda/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: andromeda_cms
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.9.0
|
|
5
5
|
platform: x86_64-linux
|
|
6
6
|
authors:
|
|
7
7
|
- LIMHAUS Inc.
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-12-03 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: railties
|
|
@@ -56,16 +56,22 @@ dependencies:
|
|
|
56
56
|
name: tomlrb
|
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
|
58
58
|
requirements:
|
|
59
|
-
- - "
|
|
59
|
+
- - ">="
|
|
60
60
|
- !ruby/object:Gem::Version
|
|
61
61
|
version: '2.0'
|
|
62
|
+
- - "<"
|
|
63
|
+
- !ruby/object:Gem::Version
|
|
64
|
+
version: '4'
|
|
62
65
|
type: :runtime
|
|
63
66
|
prerelease: false
|
|
64
67
|
version_requirements: !ruby/object:Gem::Requirement
|
|
65
68
|
requirements:
|
|
66
|
-
- - "
|
|
69
|
+
- - ">="
|
|
67
70
|
- !ruby/object:Gem::Version
|
|
68
71
|
version: '2.0'
|
|
72
|
+
- - "<"
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '4'
|
|
69
75
|
- !ruby/object:Gem::Dependency
|
|
70
76
|
name: json
|
|
71
77
|
requirement: !ruby/object:Gem::Requirement
|