Skip to content

Commit

Permalink
Add fonts config option
Browse files Browse the repository at this point in the history
  • Loading branch information
clairezed committed Feb 23, 2024
1 parent dc05e49 commit c995de7
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.10.2

* Claire Zuliani added config settings for MJML fonts.

## 4.10.1

* Fixed the binary check when using MRML to avoid warnings.
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,18 @@ MJML-Rails has the following settings with defaults:

- `use_mrml: false`
Enabling this will allow you to use Rust implementation of MJML via the `mrml` gem. It comes with prebuilt binaries instead of having to install MJML along with Node. When enabled the options `mjml_binary_version_supported`, `mjml_binary`, `minify`, `beautify` and `validation_level` are ignored.

- `fonts`
```ruby
fonts: {
'Open Sans': 'https://fonts.googleapis.com/css?family=Open+Sans:300,400,500,700',
'Droid Sans': 'https://fonts.googleapis.com/css?family=Droid+Sans:300,400,500,700',
Lato: 'https://fonts.googleapis.com/css?family=Lato:300,400,500,700',
Roboto: 'https://fonts.googleapis.com/css?family=Roboto:300,400,500,700',
Ubuntu: 'https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700',
}.to_json
```
Default fonts imported in the HTML rendered by MJML, in JSON

```ruby
# config/initializers/mjml.rb
Expand All @@ -174,6 +186,9 @@ Mjml.setup do |config|
# Use custom MJML binary with custom version
config.mjml_binary = "/path/to/custom/mjml"
config.mjml_binary_version_supported = "3.3.5"

# Use default system fonts instead of google fonts
config.fonts = {}
end
```

Expand Down
12 changes: 12 additions & 0 deletions lib/mjml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
require 'mjml/mrml_parser'
require 'mjml/railtie' if defined?(Rails)

# rubocop:disable Metrics/ModuleLength
module Mjml
mattr_accessor \
:beautify,
:minify,
:fonts,
:mjml_binary,
:mjml_binary_error_string,
:mjml_binary_version_supported,
Expand All @@ -29,6 +31,15 @@ module Mjml
' have you run $ npm install mjml?'
self.beautify = true
self.minify = false
self.fonts = {
'Open Sans':
'https://fonts.googleapis.com/css?family=Open+Sans:300,400,500,700',
'Droid Sans':
'https://fonts.googleapis.com/css?family=Droid+Sans:300,400,500,700',
Lato: 'https://fonts.googleapis.com/css?family=Lato:300,400,500,700',
Roboto: 'https://fonts.googleapis.com/css?family=Roboto:300,400,500,700',
Ubuntu: 'https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700'
}
self.validation_level = 'strict'
self.use_mrml = false

Expand Down Expand Up @@ -128,4 +139,5 @@ def logger
end
end
end
# rubocop:enable Metrics/ModuleLength
end
12 changes: 9 additions & 3 deletions lib/mjml/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def render
file.write(input)
file # return tempfile from block so #unlink works later
end
run(in_tmp_file.path, Mjml.beautify, Mjml.minify, Mjml.validation_level)
run(in_tmp_file.path, Mjml.beautify, Mjml.minify, Mjml.validation_level, Mjml.fonts)
rescue StandardError
raise if Mjml.raise_render_exception

Expand All @@ -36,10 +36,14 @@ def render
#
# @return [String] The result as string
# rubocop:disable Style/OptionalBooleanParameter: Fixing this offense would imply a change in the public API.
def run(in_tmp_file, beautify = true, minify = false, validation_level = 'strict')
# rubocop:disable Metrics/MethodLength:
# rubocop:disable Metrics/ParameterLists:
def run(in_tmp_file, beautify = true, minify = false, validation_level = 'strict', fonts = {})
Tempfile.create(['out', '.html']) do |out_tmp_file|
command = "-r #{in_tmp_file} -o #{out_tmp_file.path} " \
"--config.beautify #{beautify} --config.minify #{minify} --config.validationLevel #{validation_level}"
"--config.beautify #{beautify} --config.minify #{minify} " \
"--config.validationLevel #{validation_level} --config.fonts '#{fonts.to_json}'"

_, stderr, status = Mjml.run_mjml(command)

unless status.success?
Expand All @@ -52,6 +56,8 @@ def run(in_tmp_file, beautify = true, minify = false, validation_level = 'strict
out_tmp_file.read
end
end
# rubocop:enable Metrics/ParameterLists
# rubocop:enable Metrics/MethodLength
# rubocop:enable Style/OptionalBooleanParameter
end
end
18 changes: 17 additions & 1 deletion test/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,44 @@
end
end

describe 'can read beautify, minify, and validation_level configs' do
describe 'can read beautify, minify, fonts and validation_level configs' do
it 'uses defaults if no config is set' do
expect(Mjml.beautify).must_equal(true)
expect(Mjml.minify).must_equal(false)
expect(Mjml.validation_level).must_equal('strict')
expect(Mjml.fonts).must_equal({
'Open Sans': 'https://fonts.googleapis.com/css?family=Open+Sans:300,400,500,700',
'Droid Sans': 'https://fonts.googleapis.com/css?family=Droid+Sans:300,400,500,700',
Lato: 'https://fonts.googleapis.com/css?family=Lato:300,400,500,700',
Roboto: 'https://fonts.googleapis.com/css?family=Roboto:300,400,500,700',
Ubuntu: 'https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700'
})
end

it 'uses setup config' do
Mjml.setup do |config|
config.beautify = false
config.minify = true
config.validation_level = 'soft'
config.fonts = { Mononoki: 'https://cdn.jsdelivr.net/npm/@xz/fonts@1/serve/mononoki.min.css' }
end

expect(Mjml.beautify).must_equal(false)
expect(Mjml.minify).must_equal(true)
expect(Mjml.validation_level).must_equal('soft')
expect(Mjml.fonts).must_equal({ Mononoki: 'https://cdn.jsdelivr.net/npm/@xz/fonts@1/serve/mononoki.min.css' })

Mjml.setup do |config|
config.beautify = true
config.minify = false
config.validation_level = 'strict'
config.fonts = {
'Open Sans': 'https://fonts.googleapis.com/css?family=Open+Sans:300,400,500,700',
'Droid Sans': 'https://fonts.googleapis.com/css?family=Droid+Sans:300,400,500,700',
Lato: 'https://fonts.googleapis.com/css?family=Lato:300,400,500,700',
Roboto: 'https://fonts.googleapis.com/css?family=Roboto:300,400,500,700',
Ubuntu: 'https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700'
}
end
end
end
Expand Down

0 comments on commit c995de7

Please sign in to comment.