From 5c5cd296477bee6eb6ce38e32051968291995d44 Mon Sep 17 00:00:00 2001 From: Andreas Wagner Date: Thu, 22 Sep 2022 05:07:31 +0300 Subject: [PATCH] feat: make server configurable (#11) Add's `server` as a configuration option for using self-hosted Plausible instances. Co-authored-by: Andrew Mason --- README.md | 10 +++++- Rakefile | 2 +- bridgetown-plausible.gemspec | 2 +- bridgetown.automation.rb | 23 +++++++++---- demo/Gemfile | 2 +- demo/bridgetown.config.yml | 10 +++++- demo/plugins/site_builder.rb | 1 - lib/bridgetown-plausible/builder.rb | 9 ++--- spec/bridgetown-plausible_spec.rb | 52 ++++++++++++++++++++++++----- 9 files changed, 87 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 7f21da7..a7a28aa 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ or add manually in `Gemfile`: ```ruby group :bridgetown_plugins do - gem "bridgetown-plausible", "~> 1.0.2" + gem "bridgetown-plausible", "~> 1.1.0" end ``` @@ -74,6 +74,14 @@ plausible: # Type: String # Required: true domain: example.com + # Your Plausible instance domain. + # Only set this if you are self-hosting Plausible on your own domain. + # Requires https. + # + # Type: String + # Required: false + # Default: "plausible.io" + server: selfhosted-plausible.com ``` ## Usage diff --git a/Rakefile b/Rakefile index ee617bb..b6ae734 100644 --- a/Rakefile +++ b/Rakefile @@ -5,4 +5,4 @@ require "rspec/core/rake_task" RSpec::Core::RakeTask.new(:spec) -task :default => :spec +task default: :spec diff --git a/bridgetown-plausible.gemspec b/bridgetown-plausible.gemspec index 3f1a587..eb0634b 100644 --- a/bridgetown-plausible.gemspec +++ b/bridgetown-plausible.gemspec @@ -8,7 +8,7 @@ Gem::Specification.new do |spec| spec.authors = ["Andrew Mason"] spec.email = ["andrewmcodes@protonmail.com"] spec.summary = "Plausible Analytics Plugin for Bridgetown" - spec.description = "A Liquid tag to add Plausible analytics to your site." + spec.description = "Provides a Liquid Tag and ERB Helper to add Plausible analytics to your Bridgetown site." spec.homepage = "https://github.com/bt-rb/#{spec.name}" spec.license = "MIT" spec.metadata = { diff --git a/bridgetown.automation.rb b/bridgetown.automation.rb index 23d4809..58781c7 100644 --- a/bridgetown.automation.rb +++ b/bridgetown.automation.rb @@ -1,15 +1,26 @@ say_status :plausible, "Installing the bridgetown-plausible plugin..." domain_name = ask("What's your Plausible domain?") - +server_name = ask("If you are self-hosting Plausible, what's your instance domain? Leave blank if not self-hosting to default to plausible.io") add_bridgetown_plugin "bridgetown-plausible" -append_to_file "bridgetown.config.yml" do - <<~YAML +if server_name == "" + append_to_file "bridgetown.config.yml" do + <<~YAML + + plausible: + domain: #{domain_name} + YAML + end +else + append_to_file "bridgetown.config.yml" do + <<~YAML - plausible: - domain: #{domain_name} - YAML + plausible: + domain: #{domain_name} + server: #{server_name} + YAML + end end say_status :plausible, "All set! Double-check the plausible block in your config file and review docs at" diff --git a/demo/Gemfile b/demo/Gemfile index 4ccb5a1..ecfd876 100644 --- a/demo/Gemfile +++ b/demo/Gemfile @@ -16,7 +16,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" } # # Happy Bridgetowning! -gem "bridgetown", "~> 0.19.2" +gem "bridgetown", "~> 1.1.0" group :bridgetown_plugins do gem "bridgetown-plausible", path: "../" end diff --git a/demo/bridgetown.config.yml b/demo/bridgetown.config.yml index 730dfbc..f914302 100644 --- a/demo/bridgetown.config.yml +++ b/demo/bridgetown.config.yml @@ -27,4 +27,12 @@ plausible: # # Type: String # Required: true - domain: example.com + domain: tracked-site.com + # Your Plausible instance domain. + # Only set this if you are self-hosting Plausible on your own domain. + # Requires https. + # + # Type: String + # Required: false + # Default: "plausible.io" + server: selfhosted-plausible.com diff --git a/demo/plugins/site_builder.rb b/demo/plugins/site_builder.rb index eaa7c16..efe971c 100644 --- a/demo/plugins/site_builder.rb +++ b/demo/plugins/site_builder.rb @@ -1,4 +1,3 @@ class SiteBuilder < Bridgetown::Builder # write builders which subclass SiteBuilder in plugins/builder end - diff --git a/lib/bridgetown-plausible/builder.rb b/lib/bridgetown-plausible/builder.rb index 056b12e..be00333 100644 --- a/lib/bridgetown-plausible/builder.rb +++ b/lib/bridgetown-plausible/builder.rb @@ -17,12 +17,13 @@ def build def render domain = options.dig(:domain)&.strip + server = options.dig(:server)&.strip || "plausible.io" tag = if domain - markup_for_domain(domain) + markup_for_snippet(domain, server) else Bridgetown.logger.warn "Plausible", "Domain not configured." - markup_for_domain("NOT CONFIGURED") + markup_for_snippet("NOT CONFIGURED", server) end return wrap_with_comment(tag) unless Bridgetown.environment.production? @@ -30,8 +31,8 @@ def render tag end - def markup_for_domain(domain) - "" + def markup_for_snippet(domain, server) + "" end def wrap_with_comment(tag) diff --git a/spec/bridgetown-plausible_spec.rb b/spec/bridgetown-plausible_spec.rb index b3874f8..1208b1d 100644 --- a/spec/bridgetown-plausible_spec.rb +++ b/spec/bridgetown-plausible_spec.rb @@ -21,13 +21,22 @@ end context "when the rendering engine is liquid" do - let(:contents) { File.read(dest_dir("liquid.html")) } + let(:contents) { File.read(dest_dir("liquid/index.html")) } context "when the domain is configured" do - let(:overrides) { {"plausible" => {"domain" => "example.com"}} } + let(:overrides) { {"plausible" => {"domain" => "tracked-site.com"}} } + it "outputs the correct HTML" do + expect(contents).to match <<~HTML + + HTML + end + end + + context "when server && domain are configured" do + let(:overrides) { {"plausible" => {"server" => "selfhosted-plausible.com", "domain" => "tracked-site.com"}} } it "outputs the correct HTML" do expect(contents).to match <<~HTML - + HTML end end @@ -42,13 +51,22 @@ end context "when the rendering engine is erb" do - let(:contents) { File.read(dest_dir("erb.html")) } + let(:contents) { File.read(dest_dir("erb/index.html")) } context "when the domain is configured" do - let(:overrides) { {"plausible" => {"domain" => "example.com"}} } + let(:overrides) { {"plausible" => {"domain" => "tracked-site.com"}} } + it "outputs the correct HTML" do + expect(contents).to match <<~HTML + + HTML + end + end + + context "when server && domain are configured" do + let(:overrides) { {"plausible" => {"server" => "selfhosted-plausible.com", "domain" => "tracked-site.com"}} } it "outputs the correct HTML" do expect(contents).to match <<~HTML - + HTML end end @@ -70,7 +88,7 @@ end context "when the rendering engine is liquid" do - let(:contents) { File.read(dest_dir("liquid.html")) } + let(:contents) { File.read(dest_dir("liquid/index.html")) } context "when the domain is configured" do let(:overrides) { {"plausible" => {"domain" => "example.com"}} } @@ -81,6 +99,15 @@ end end + context "when server && domain are configured" do + let(:overrides) { {"plausible" => {"server" => "selfhosted-plausible.com", "domain" => "tracked-site.com"}} } + it "outputs the correct HTML" do + expect(contents).to match <<~HTML + + HTML + end + end + context "when the domain is not configured" do it "outputs the correct HTML" do expect(contents).to match <<~HTML @@ -91,7 +118,7 @@ end context "when the rendering engine is erb" do - let(:contents) { File.read(dest_dir("erb.html")) } + let(:contents) { File.read(dest_dir("erb/index.html")) } context "when the domain is configured" do let(:overrides) { {"plausible" => {"domain" => "example.com"}} } @@ -102,6 +129,15 @@ end end + context "when server && domain are configured" do + let(:overrides) { {"plausible" => {"server" => "selfhosted-plausible.com", "domain" => "tracked-site.com"}} } + it "outputs the correct HTML" do + expect(contents).to match <<~HTML + + HTML + end + end + context "when the domain is not configured" do it "outputs the correct HTML" do expect(contents).to match <<~HTML