Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better code styling (back to Rouge) #399

Merged
merged 9 commits into from
Dec 4, 2023
Merged
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
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ gem "rails", "7.1.1"

gem "blazer", "~> 3.0"
gem "bootsnap", require: false # Reduces boot times through caching; required in config/boot.rb
gem "commonmarker", ">= 1.0.0.pre11", "< 2"
gem "commonmarker", "~> 0.23.10", "< 1"
gem "devise", "~> 4.9"
gem "good_job", "~> 3.21"
gem "humanize", "~> 2.5"
Expand All @@ -19,6 +19,7 @@ gem "omniauth-rails_csrf_protection", "~> 1.0"
gem "omniauth-slack-openid"
gem "pg", "~> 1.5"
gem "puma", "~> 6.4"
gem "rouge", "~> 4.2"
gem "sentry-rails", "~> 5.13"
gem "sentry-ruby", "~> 5.13"
gem "slack-ruby-client"
Expand Down
8 changes: 4 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ GEM
thor (~> 1.0)
byebug (11.1.3)
chartkick (5.0.4)
commonmarker (1.0.0.pre11-arm64-darwin)
commonmarker (1.0.0.pre11-x86_64-darwin)
commonmarker (1.0.0.pre11-x86_64-linux)
commonmarker (0.23.10)
concurrent-ruby (1.2.2)
connection_pool (2.4.1)
crack (0.4.5)
Expand Down Expand Up @@ -319,6 +317,7 @@ GEM
reverse_markdown (2.1.1)
nokogiri
rexml (3.2.6)
rouge (4.2.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
Expand Down Expand Up @@ -468,7 +467,7 @@ DEPENDENCIES
brakeman (~> 6.0)
bundler-audit (~> 0.9)
byebug
commonmarker (>= 1.0.0.pre11, < 2)
commonmarker (~> 0.23.10, < 1)
devise (~> 4.9)
dotenv-rails (~> 2.8)
erb_lint (~> 0.5)
Expand All @@ -485,6 +484,7 @@ DEPENDENCIES
puma (~> 6.4)
rack-mini-profiler (~> 3.1)
rails (= 7.1.1)
rouge (~> 4.2)
rspec-rails (~> 6.0)
rubocop (~> 1.56)
rubocop-performance (~> 1.19)
Expand Down
2 changes: 2 additions & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/*= require rouge */
/*= require tailwind */
16 changes: 8 additions & 8 deletions app/assets/stylesheets/application.tailwind.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,33 @@
@apply px-1 bg-aoc-gray-darkest border border-aoc-gray-darker rounded;
Aquaj marked this conversation as resolved.
Show resolved Hide resolved
}

.code-highlight * {
.snippet * {
@apply last:mb-0
}

.code-highlight h1 {
.snippet h1 {
@apply my-4 border-b border-aoc-gray-darker pb-2 font-semibold text-4xl
}

.code-highlight h2 {
.snippet h2 {
@apply my-4 border-b border-aoc-gray-darker pb-1.5 font-semibold text-2xl
}

.code-highlight h3 {
.snippet h3 {
@apply my-4 font-semibold text-lg
}

.code-highlight pre {
.snippet pre {
@apply my-4 relative pt-2 !bg-dark border border-aoc-gray-darker rounded;
}

.code-highlight pre::before {
@apply absolute top-0 right-0 border-b border-l rounded-bl border-aoc-gray-darker px-2 capitalize font-semibold;
.snippet pre.code-highlighter::before {
@apply absolute top-0 right-0 border-b border-l rounded-bl border-aoc-gray-darker px-2 font-semibold;

content: attr(lang);
}

.code-highlight pre code {
pre.code-highlighter {
@apply block w-full p-4 overflow-x-auto scrollbar-chrome text-xs;
}

Expand Down
1 change: 1 addition & 0 deletions app/assets/stylesheets/rouge.css.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= Rouge::Theme.find("molokai").render(scope: ".code-highlighter") %>
2 changes: 1 addition & 1 deletion app/components/snippets/box_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<% end %>
</div>

<pre class="code-highlight break-words text-sm whitespace-normal" data-language="<%= @snippet.language %>">
<pre class="snippet break-words text-sm whitespace-normal" data-language="<%= @snippet.language %>">
<%= raw render_markdown @snippet.code, default_language: @snippet.language %>
</pre>

Expand Down
24 changes: 14 additions & 10 deletions app/helpers/application_helper.rb
Aquaj marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@ def messages_sanitizer(text)
sanitize(text, tags: %w[b em del span sub sup], attributes: %w[style])
end

DEFAULT_THEME = "base16-ocean.dark"

def render_markdown(commonmarkdown, default_language: nil)
config = {}
ast = CommonMarker.render_doc(commonmarkdown)
ast.walk do |node|
next unless node.type == :code_block

config[:options] = {}
config[:options][:parse] = { default_info_string: default_language }.compact
config[:options][:render] = { escape: true, github_pre_lang: true }
language = node.fence_info.presence || default_language
lexer = ::Rouge::Lexer.find_fancy(language) || ::Rouge::Lexers::PlainText.new

config[:plugins] = {
syntax_highlighter: { theme: DEFAULT_THEME }
Aquaj marked this conversation as resolved.
Show resolved Hide resolved
}
formatter = Rouge::Formatters::HTML.new
code_html = formatter.format(lexer.lex(node.string_content))
html = %(<pre class="code-highlighter" lang="#{lexer.class.title}">#{code_html}</pre>)
new_node = ::CommonMarker::Node.new(:html)
new_node.string_content = html

Commonmarker.to_html(commonmarkdown, **config)
node.insert_before(new_node)
node.delete
end
ast.to_html(:UNSAFE)
end
end
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<%= csrf_meta_tags %>
<%= csp_meta_tag %>

<%= stylesheet_link_tag "tailwind", "data-turbo-track": "reload" %>
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
pil0u marked this conversation as resolved.
Show resolved Hide resolved
<%= javascript_importmap_tags %>
</head>

Expand Down