app/models/user.rb
A snippet() snippet
snippet()
is a SQLite function that returns a snippet of text containing the search term. It’s used to highlight search results. This example shows an ActiveRecord scope that enables selection additional fields title_snippet
and body_snippet
from an FTS5 virtual table (created separately).
https://www.sqlite.org/fts5.html#the_snippet_function
scope :with_snippets do
select("pages.*")
.select("snippet(pages_search_index, 0, '<mark>', '</mark>', '…', 32) AS title_snippet")
.select("snippet(pages_search_index, 1, '<mark>', '</mark>', '…', 32) AS body_snippet")
end
footer.css
CSS :has() Example
Hey Tailwind enthusiasts, don‘t sleep on CSS. It‘s getting really good. Modern browsers now support features like custom variables, new pseudo selectors (e.g. :has(), :is()), and CSS nesting. You can do some sweet styling without need of a preprocessor like SASS. Here‘s a snippet from the footer nav on https://www.joyofrails.com.
nav li:has(+ li) {
&:after {
margin: 0 var(--space-xs);
content: ' / ';
}
}
Markdown ASTs with Commonmarker
For converting Markdown, Rubyists may default to Redcarpet or Kramdown. I suggest taking a look at Commonmarker, a Ruby gem built on top of comrak, a Rust of implementation the GitHub-flavored version of the CommonMark spec.
Why? One reason is you can parse Markdown into an abstract syntax tree (AST) which enables powerful customization of what you can do with the markdown source.
require 'commonmarker'
Commonmarker.to_html('"Hi *there*"', options: {
parse: { smart: true }
})
# => <p>“Hi <em>there</em>”</p>\n
doc = Commonmarker.parse("*Hello* world", options: {
parse: { smart: true }
})
doc.walk do |node|
puts node.type # => [:document, :paragraph, :emph, :text, :text]
end
Vite on Rails
An open secret about Rails and JavaScript: lots of apps are using Vite to bundle frontend assets via the excellent Rails integration provided by Vite Ruby. You may want to give it a try. #rails #vite
bundle add vite_rails
bundle exec vite install