npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@ayushn21/actiontext-syntax-highlighter

v0.1.3

Published

Extends ActionText to support highlighed code blocks

Downloads

5

Readme

Description

actiontext-syntax-highlighter is an extension for rails/actiontext to allow the user to add language specified code blocks that are then highlighted on the server using Rouge.

Check out the demo app to see how it works!.

Note: The experience is a bit flaky, especially on Safari. If you have any suggestions for how to improve it, please file an issue; or even better, open a Pull Request! I recommend using Firefox to test this out as that's where it works best!

How it works

This library intercepts the toolbar code button in the Trix editor and creates an attachment containing the HTML for an editable code block and language selection dropdown.

The changes to the code block are saved to the server every time edits are made. When the code block is being rendered out for viewing, it uses Rouge to highlight the content using the language selected in the dropdown; or attempting to guess the language if it's missing.

Installation

Install ActionText if you haven't already done so:

$ bin/rails action_text:install

Then add this gem to your application's Gemfile:

gem 'actiontext-syntax-highlighter', require: 'action_text_syntax_highlighter/engine'

And bundle and install it:

$ bundle
$ bin/rails action_text_syntax_highlighter:install

This will copy the database migrations into place and install the Yarn module for the frontend code.

Usage

Add the following lines to your application.js:

import { ActionTextSyntaxHighlighter } from "@ayushn21/actiontext-syntax-highlighter"
ActionTextSyntaxHighlighter.start()

Import the CSS for a theme from the themes directory:

import "@ayushn21/actiontext-syntax-highlighter/themes/github.css"

The default theme is set to GitHub. If you wish to use another theme, import its CSS file and set the following config option in your application.rb:

config.action_text_syntax_highlighter.default_theme = :base16

You can override the default theme by setting @highlighted_code_block_theme in your controller action where you're rendering out the rich text.

ActionTextSyntaxHighlighter requires a global Trix variable to create attachments, so require Trix in your application as below:

window.Trix = require("trix")

Finally, add a data-highlights-code-blocks='true' attribute to the Trix editor where you'd like to use this plugin:

<%= f.rich_text_area :content, data: { highlights_code_blocks: true } %>

Bundled JavaScript

If you wish, you can import pre-bundled JavaScript instead of the module as described above:

import "@ayushn21/actiontext-syntax-highlighter/dist"

Converting existing rich texts

If you've already code rich texts in your app with code blocks using a pre tag, you can use the method convert_pre_tags_to_highlighted_code_blocks on ActionText::RichText to migrate them over to highlighted code blocks.

For example:

ActionText::RichText.find_each do |rich_text|
  rich_text.convert_pre_tags_to_highlighted_code_blocks
end

Cleaning up deleted code blocks

ActionTextSyntaxHighlighter nullifies the rich_text_id on a HighlightedCodeBlock when it's removed from an ActionText::RichText. However it doesn't delete the record.

I recommend you set up a cron job to execute the included ActionTextSyntaxHighlighter::PurgeDeletedHighlightedCodeBlocksJob which will delete all HighlightedCodeBlocks over a day old without an associated RichText.

Contributing

  1. Fork it (https://github.com/ayushn21/actiontext-syntax-highlighter/fork)
  2. Clone the fork using git clone to your local development machine.
  3. Create your feature branch (git checkout -b my-new-feature)
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create a new Pull Request

If you've made a change to the frontend code, please also run rake frontend:build before opening your PR!

Info

This gem was extracted from chapter24.app

License

The gem is available as open source under the terms of the MIT License.