forked from nickchomey/discourse-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.rb
117 lines (96 loc) · 3.73 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# frozen_string_literal: true
# name: discourse-docs
# about: A plugin to make it easy to explore and find knowledge base documents in Discourse
# version: 0.1
# author: Justin DiRose
# url: https://github.com/discourse/discourse-docs
# transpile_js: true
enabled_site_setting :docs_enabled
register_asset "stylesheets/common/docs.scss"
register_asset "stylesheets/mobile/docs.scss"
register_svg_icon "sort-alpha-down"
register_svg_icon "sort-alpha-up"
register_svg_icon "sort-numeric-up"
register_svg_icon "sort-numeric-down"
register_svg_icon "far-circle"
load File.expand_path("lib/docs/engine.rb", __dir__)
load File.expand_path("lib/docs/query.rb", __dir__)
GlobalSetting.add_default :docs_path, "docs"
module ::Docs
PLUGIN_NAME = "discourse-docs"
end
after_initialize do
require_dependency "search"
if SiteSetting.docs_enabled
if Search.respond_to? :advanced_filter
Search.advanced_filter(/in:(kb|docs)/) do |posts|
selected_categories = SiteSetting.docs_categories.split("|")
if selected_categories
categories = Category.where("id IN (?)", selected_categories).pluck(:id)
end
selected_tags = SiteSetting.docs_tags.split("|")
tags = Tag.where("name IN (?)", selected_tags).pluck(:id) if selected_tags
posts.where(
"category_id IN (?) OR topics.id IN (SELECT DISTINCT(tt.topic_id) FROM topic_tags tt WHERE tt.tag_id IN (?))",
categories,
tags,
)
end
end
end
if Oneboxer.respond_to?(:register_local_handler)
Oneboxer.register_local_handler("docs/docs") do |url, route|
uri = URI(url)
query = URI.decode_www_form(uri.query).to_h if uri.query
if query && query["topic"]
topic = Topic.includes(:tags).find_by(id: query["topic"])
if Docs.topic_in_docs(topic.category_id, topic.tags) && Guardian.new.can_see_topic?(topic)
first_post = topic.ordered_posts.first
args = {
topic_id: topic.id,
post_number: first_post.post_number,
avatar: PrettyText.avatar_img(first_post.user.avatar_template_url, "tiny"),
original_url: url,
title: PrettyText.unescape_emoji(CGI.escapeHTML(topic.title)),
category_html: CategoryBadge.html_for(topic.category),
quote:
PrettyText.unescape_emoji(
first_post.excerpt(SiteSetting.post_onebox_maxlength, keep_svg: true),
),
}
template = Oneboxer.template("discourse_topic_onebox")
Mustache.render(template, args)
end
else
args = { url: url, name: I18n.t("js.docs.title") }
Mustache.render(Docs.onebox_template, args)
end
end
end
if InlineOneboxer.respond_to?(:register_local_handler)
InlineOneboxer.register_local_handler("docs/docs") do |url, route|
uri = URI(url)
query = URI.decode_www_form(uri.query).to_h if uri.query
if query && query["topic"]
topic = Topic.includes(:tags).find_by(id: query["topic"])
if Docs.topic_in_docs(topic.category_id, topic.tags) && Guardian.new.can_see_topic?(topic)
{ url: url, title: topic.title }
end
else
{ url: url, title: I18n.t("js.docs.title") }
end
end
end
add_to_class(:topic_query, :list_docs_topics) { default_results(@options) }
on(:robots_info) do |robots_info|
robots_info[:agents] ||= []
any_user_agent = robots_info[:agents].find { |info| info[:name] == "*" }
if !any_user_agent
any_user_agent = { name: "*" }
robots_info[:agents] << any_user_agent
end
any_user_agent[:disallow] ||= []
any_user_agent[:disallow] << "/#{GlobalSetting.docs_path}/"
end
add_to_serializer(:site, :docs_path) { GlobalSetting.docs_path }
end