GithubHelp home page GithubHelp logo

bridgetownrb / bridgetown-quick-search Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 8.0 100 KB

A Liquid + Web Component for Bridgetown sites which performs search queries with Lunr.js.

License: MIT License

Ruby 28.61% Liquid 5.33% JavaScript 62.55% Shell 3.51%
bridgetown bridgetown-plugin

bridgetown-quick-search's Introduction

Bridgetown Quick Search plugin

This Bridgetown plugin provides a component you can add to your site (likely the top navbar) to allow fast, real-time search of your posts, pages, and other collection documents. It automatically generates a JSON index file which gets built along with the rest of the site, and then the component consumes that file and uses Lunr.js to construct the live search index and provide the search results as you type.


Installation for Bridgetown 1.2+

Run this command to add this plugin to your site's Gemfile:

$ bundle add bridgetown-quick-search

And then add the initializer to your configuration in config/initializers.rb:

init :"bridgetown-quick-search"

(For Bridgetown 1.1 or earlier, read these instructions.)

Next, add this line to the top of your frontend's Javascript index file:

// frontend/javascript/index.js

import "bridgetown-quick-search/dist"

Then add the Liquid component to one of your site templates, for example src/_components/navbar.liquid:

{% render "bridgetown_quick_search/search" %}

If you're using a Ruby-based template (ERB, etc.), you can use the liquid_render helper:

<%= liquid_render "bridgetown_quick_search/search" %>

Component Options

You can provide additional Liquid variables to the component to configure its appearance. These are:

  • placeholder: Text that will appear in the input control when no search value is present
  • input_class: Add custom CSS class names to the input control
  • theme: The component's default theme is a "light" appearance, but you can also set it to use a "dark" appearance
  • snippet_length: The length of the text snippet for each search result. Defaults to 142.

Here's an example of using all variables at once:

{% render "bridgetown_quick_search/search", placeholder: "Search", input_class: "input", theme: "dark", snippet_length: 200 %}

Styling

You can use CSS variables to control aspects of the search results popup. The popup is rendered inside of a Web Component using Shadow DOM, so these variables are the primary method of altering its appearance. The available variables are link-color, divider-color, text-color, border-radius and border-corner-radius.

bridgetown-search-results {
  --link-color: #0f5348;
  --divider-color: #e6e1d7;
  --text-color: #3e3f3a;
}

You can also alter the outer popup container via the custom element directly, as well as the wrapper surrounding the results contents using CSS Shadow Parts:

bridgetown-search-results {
/* Adjust the outer container of the popup */
}

bridgetown-search-results::part(inner) {
/* Adjust the popup contents wrapper */
}

bridgetown-search-results::part(inner-link) {
/* Adjust the link style of each search result */
}

Controlling the Search Index

You can add exclude_from_search: true to the front matter of any page or document to exclude it from the search index JSON. To batch-exclude pages, collections, categories, etc., you could use front matter defaults to set the exclude_from_search variable.

Testing

  • Run bundle exec rspec to run the test suite
  • Or run script/cibuild to validate with Rubocop and test with rspec together.

Contributing

  1. Fork it (https://github.com/bridgetownrb/bridgetown-quick-search/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

bridgetown-quick-search's People

Contributors

dependabot[bot] avatar jaredcwhite avatar konnorrogers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

bridgetown-quick-search's Issues

bridgetown 0.21.4 errors with quick-search 1.1.1

This is the output from yarn start. The server runs but the search box does nothing.

spyle:llc/bridgetown|master> yarn start
yarn run v1.22.10
$ node start.js
$ webpack --mode development -w
[Webpack] assets by status 288 KiB [cached] 2 assets
[Webpack] assets by status 1.19 MiB [compared for emit]
[Webpack]   asset ../frontend/fonts/forkawesome-webfont-86fe294b5591541c133d4b00ba6d4937.svg 547 KiB [compared for emit] (auxiliary name: main)
[Webpack]   asset ../fonts/forkawesome-webfont-266813812be54111803053c2fabcce1b.eot 214 KiB [compared for emit] (auxiliary name: main)
[Webpack]   asset ../fonts/forkawesome-webfont-94d78a2cca630819d56bc25d70b5db55.ttf 214 KiB [compared for emit] (auxiliary name: main)
[Webpack]   asset ../fonts/forkawesome-webfont-2350c61717129050edc637b93feb0d06.woff 133 KiB [compared for emit] (auxiliary name: main)
[Webpack]   asset ../fonts/forkawesome-webfont-fafb863d51da3f819fea3f51f402da66.woff2 107 KiB [compared for emit] (auxiliary name: main)
[Webpack] asset ../../../../.bridgetown-webpack/manifest.json 815 bytes [emitted]
[Webpack] Entrypoint main 288 KiB (1.47 MiB) = ../css/main.60a971c866a980b4dd1d.css 281 KiB main.9b5b3cefe070fab145df.js 7.04 KiB 7 auxiliary assets
[Webpack]
[Webpack] ERROR in ./frontend/javascript/index.js 3:0-47
[Webpack] Module not found: Error: Package path ./frontend/dist is not exported from package /Users/spyle/projects/llc/bridgetown/node_modules/bridgetown-quick-search (see exports field in /Users/spyle/projects/llc/bridgetown/node_modules/bridgetown-quick-search/package.json)
[Webpack]
[Webpack] ERROR in ./node_modules/bridgetown-quick-search/frontend/javascript/index.js
[Webpack] Module build failed (from ./node_modules/esbuild-loader/dist/index.js):
[Webpack] Error: Transform failed with 1 error:
[Webpack] /Users/spyle/projects/llc/bridgetown/node_modules/bridgetown-quick-search/frontend/javascript/index.js:21:0: error: Unexpected "@"
[Webpack]     at failureErrorWithLog (/Users/spyle/projects/llc/bridgetown/node_modules/esbuild/lib/main.js:1475:15)
[Webpack]     at /Users/spyle/projects/llc/bridgetown/node_modules/esbuild/lib/main.js:1264:29
[Webpack]     at /Users/spyle/projects/llc/bridgetown/node_modules/esbuild/lib/main.js:611:9
[Webpack]     at handleIncomingPacket (/Users/spyle/projects/llc/bridgetown/node_modules/esbuild/lib/main.js:708:9)
[Webpack]     at Socket.readFromStdout (/Users/spyle/projects/llc/bridgetown/node_modules/esbuild/lib/main.js:578:7)
[Webpack]     at Socket.emit (events.js:315:20)
[Webpack]     at addChunk (_stream_readable.js:302:12)
[Webpack]     at readableAddChunk (_stream_readable.js:278:9)
[Webpack]     at Socket.Readable.push (_stream_readable.js:217:10)
[Webpack]     at Pipe.onStreamRead (internal/stream_base_commons.js:186:23)
[Webpack]  @ ./node_modules/bulmatown/frontend/javascript/index.js 1:0-33
[Webpack]  @ ./frontend/javascript/index.js 2:0-34
[Webpack]
[Webpack] webpack 5.55.0 compiled with 2 errors
$ bundle exec bridgetown serve --port 4001
[Bridgetown]           Starting: Bridgetown v0.21.4 (codename "Broughton Beach")
[Bridgetown]        Environment: development
[Bridgetown]             Source: /Users/spyle/projects/llc/bridgetown/src
[Bridgetown]        Destination: /Users/spyle/projects/llc/bridgetown/output
[Bridgetown]     Custom Plugins: /Users/spyle/projects/llc/bridgetown/plugins
[Bridgetown]  Incremental build: disabled. Enable with --incremental
[Bridgetown]         Generating...
[Bridgetown]         Pagination: Complete, processed 1 pagination page(s)
[Bridgetown]    Bridgetown Feed: Generating feed for posts
[Bridgetown]             Done! ๐ŸŽ‰ Completed in less than 0.23 seconds.
[Bridgetown]  Auto-regeneration: enabled.
[Bridgetown]     Server address: http://127.0.0.1:4001
[Bridgetown]     Server running... press ctrl-c to stop.
$ node sync.js
[Live] [Browsersync] Proxying: http://localhost:4001
[Live] [Browsersync] Access URLs:
[Live]  --------------------------------------
[Live]        Local: http://localhost:4000
[Live]     External: http://172.16.14.137:4000
[Live]  --------------------------------------
[Live]           UI: http://localhost:4002
[Live]  UI External: http://localhost:4002
[Live]  --------------------------------------
[Live] [Browsersync] Watching files...

Here is my Gemfile

source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

gem "bridgetown", "~> 0.21.4"

group :bridgetown_plugins do
	gem "bulmatown", "~> 1.1"
	gem "bridgetown-feed", "~> 2.0.1"
	gem "bridgetown-quick-search", "~> 1.1.1"
end

Here is my package.json

{
  "name": "bridgetown",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "build": "bundle exec bridgetown build",
    "serve": "bundle exec bridgetown serve",
    "clean": "bundle exec bridgetown clean",
    "webpack-build": "webpack --mode production",
    "webpack-dev": "webpack --mode development -w",
    "deploy": "yarn clean && yarn webpack-build && yarn build",
    "sync": "node sync.js",
    "start": "node start.js"
  },
  "devDependencies": {
    "browser-sync": "^2.26.7",
    "concurrently": "^5.2.0",
    "css-loader": "^4.3.0",
    "esbuild": "^0.12.7",
    "esbuild-loader": "^2.13.1",
    "file-loader": "^6.2.0",
    "mini-css-extract-plugin": "^1.3.1",
    "sass": "^1.32.8",
    "sass-loader": "^8.0.2",
    "webpack": "^5.39.1",
    "webpack-cli": "^4.7.2",
    "webpack-manifest-plugin": "^3.1.1",
    "webpack-merge": "^5.8.0"
  },
  "resolutions": {
    "postcss-focus-within": "^4.0.0"
  },
  "dependencies": {
    "bridgetown-quick-search": "1.1.1",
    "bulmatown": "1.1.0"
  }
}

My current workaround is to remove the config/webpack.defaults.js and reintroduce this webpack.config.js from 0.19.4 (which was a previous working instance.

const path = require("path");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const ManifestPlugin = require("webpack-manifest-plugin");

module.exports = {
	entry: "./frontend/javascript/index.js",
	devtool: "source-map",
	// Set some or all of these to true if you want more verbose logging:
	stats: {
		modules: false,
		builtAt: false,
		timings: false,
		children: false,
	},
	output: {
		path: path.resolve(__dirname, "output", "_bridgetown", "static", "js"),
		filename: "all.[contenthash].js",
	},
	resolve: {
		extensions: [".js", ".jsx"],
		modules: [
			path.resolve(__dirname, 'frontend', 'javascript'),
			path.resolve(__dirname, 'frontend', 'styles'),
			path.resolve('./node_modules')
		],
		alias: {
			bridgetownComponents: path.resolve(__dirname, "src", "_components")
		}
	},
	plugins: [
		new MiniCssExtractPlugin({
			filename: "../css/all.[contenthash].css",
		}),
		new ManifestPlugin({
			fileName: path.resolve(__dirname, ".bridgetown-webpack", "manifest.json"),
		}),
	],
	module: {
		rules: [
			{
				test: /\.(js|jsx)/,
				use: {
					loader: "babel-loader",
					options: {
						presets: ["@babel/preset-env"],
						plugins: [
							["@babel/plugin-proposal-decorators", { "legacy": true }],
							["@babel/plugin-proposal-class-properties", { "loose": true }],
							["@babel/plugin-proposal-private-methods", { "loose": true }],
							[
								"@babel/plugin-transform-runtime",
								{
									helpers: false,
								},
							],
						],
					},
				},
			},

			{
				test: /\.(s[ac]|c)ss$/,
				use: [
					MiniCssExtractPlugin.loader,
					"css-loader",
					{
						loader: "sass-loader",
						options: {
							sassOptions: {
								includePaths: [
									path.resolve(__dirname, "src/_components")
								],
							},
						},
					},
				],
			},

			{
				test: /\.woff2?$|\.ttf$|\.eot$|\.svg$/,
				loader: "file-loader",
				options: {
					outputPath: "../fonts",
					publicPath: "../fonts",
				},
			},
		],
	},
};

Add hotkeys support and enter key

  • Cmd/Ctrl+K to activate the search field
  • Hit Enter to bring up results again if they were previously hidden

I already have this working as extra code on the new Bridgetown website, but it'd be great to upstream this right into the plugin.

For prototype pages, no `content` is included in index

I think what I'm seeing here is for any Prototype pages, the content attribute in the generated Json index is blank, implying document.content is blank for these pages when the index is generated

        {
          "id": "my-article",
          "title": "Title",
          "collection": {
            "label": "notes",
            "name": "Posts"
          },
          "categories": "notes",
          "tags": "",
          "url": "/my-article/",
          "content": ""
        },

Bridgetown v1.0.0 and bridgetown-quick-search v1.1.2 - load error

Error:

bin/bridgetown start

[Frontend] esbuild: frontend bundling started...
[Frontend]  > node_modules/bridgetown-quick-search/frontend/javascript/index.js:22:0: error: Unexpected "@"
[Frontend]     22 โ”‚ @customElement("bridgetown-search-form")
[Frontend]        โ•ต ^
[Frontend] 
[Frontend] esbuild: build process error, cannot write manifest
[Frontend] error Command failed with exit code 1.
[Frontend] info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
[Frontend]   Exception raised: RuntimeError
[Frontend] Command failed with status (1): [yarn run esbuild-dev...] 
[Frontend]                  1: /Users/btihen/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/rake-13.0.6/lib/rake/file_utils.rb:67:in `block in create_shell_runner'
[Frontend]                  2: /Users/btihen/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/rake-13.0.6/lib/rake/file_utils.rb:57:in `sh'
[Frontend]                  3: /Users/btihen/Dropbox/devel/marpori/learning/ruby_kafi/bridgetown_talk/ruby_kafi_bridgetown_default_code/Rakefile:35:in `block (2 levels) in <top (required)>'
[Frontend]                  4: /Users/btihen/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `block in execute'
[Frontend]                  5: /Users/btihen/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `each'
[Frontend]          Backtrace: Use the --trace option for complete information.

Reproduce with:

bridgetown new bridgetown_search_crash
cd bridgetown_search_crash
bundle add bridgetown-quick-search -g bridgetown_plugins

# follow instructions in README
# add import to; `frontend/javascript/index.js`
import "bridgetown-quick-search/dist"
# add the Liquid component to `src/_components/navbar.liquid`
{% render "bridgetown_quick_search/search" %}

# start bridgetown:
bin/bridgetown start

# see above error report

Add a clearResults() method

Right now if you want to programmatically dismiss the results dropdown, you have to call showResults = false and results = []. We should provide a simple method instead.

bridgetown_quick_search/index.json is outside repositor

I started to get this error message in my project, it works well for a while.

fatal: /home/myuser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bridgetown-quick-search-2.0.0/content/bridgetown_quick_search/index.json: '/home/myuser/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bridgetown-quick-search-2.0.0/content/bridgetown_quick_search/index.json' is outside repository at '/home/myuser/bridgetown/site'

fatal: /home/myuser/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/bridgetown-quick-search-2.0.0/content/bridgetown_quick_search/index.json: '/home/myuser/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/bridgetown-quick-search-2.0.0/content/bridgetown_quick_search/index.json' is outside repository at '/home/myuser/bridgetown/site'

Linux/Ubuntu with rbenv

"bridgetown", "> 1.3.0"
"bridgetown-quick-search", "
> 2.0",

with
import "bridgetown-quick-search/dist"

It happens when I have this in my config: prefix_default_locale: true

Hitting ESC should work inside of menu, change <h1> to <h2>

Testing with macOS Safari + VoiceOver and keyboard navigation, seems pretty decent to get to the list of search results, but then you can't hit ESC to exit out of the results, so you sort of get stuck in there.

Also, hearing "heading level 1" for each item feels strange. Probably makes more sense as <h2> tags even though you're inside of shadow DOM.

Page code is included in search results

Whenever the search index is generated, it's coming before some of the other templates have rendered so template code is leaking into the index (and the final converted text is missing).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.