插件能力

liangliangyy 7 months ago
parent ef3efec630
commit 3e095cdf12

@ -19,6 +19,7 @@ from haystack.views import SearchView
from blog.models import Article, Category, LinkShowType, Links, Tag
from comments.forms import CommentForm
from djangoblog.plugin_manage import hooks
from djangoblog.plugin_manage.hook_constants import ARTICLE_CONTENT_HOOK_NAME
from djangoblog.utils import cache, get_blog_setting, get_sha256
logger = logging.getLogger(__name__)
@ -160,13 +161,13 @@ class ArticleDetailView(DetailView):
article = self.object
# Action Hook, 通知插件"文章详情已获取"
hooks.run_action('after_article_body_get', article=article, request=self.request)
# Filter Hook, 允许插件修改文章正文
article.body = hooks.apply_filters('the_content', article.body, article=article, request=self.request)
# toc = markdown.toc
md = markdown.Markdown(extensions=[
'markdown.extensions.extra',
])
article.body = md.convert(article.body)
# # Filter Hook, 允许插件修改文章正文
article.body = hooks.apply_filters(ARTICLE_CONTENT_HOOK_NAME, article.body, article=article, request=self.request)
# # toc = markdown.toc
# md = markdown.Markdown(extensions=[
# 'markdown.extensions.extra',
# ])
# article.body = md.convert(article.body)
return context

@ -2,3 +2,6 @@ ARTICLE_DETAIL_LOAD = 'article_detail_load'
ARTICLE_CREATE = 'article_create'
ARTICLE_UPDATE = 'article_update'
ARTICLE_DELETE = 'article_delete'
ARTICLE_CONTENT_HOOK_NAME = "the_content"

@ -345,5 +345,5 @@ for plugin_dir in os.listdir(PLUGINS_DIR):
if os.path.isdir(plugin_path) and os.path.exists(os.path.join(plugin_path, '__init__.py')):
try:
__import__(f'plugins.{plugin_dir}.plugin')
except ImportError:
pass
except ImportError as e:
print("Failed to import plugin:", plugin_dir, e)

@ -1,5 +1,6 @@
from djangoblog.plugin_manage.base_plugin import BasePlugin
from djangoblog.plugin_manage import hooks
from djangoblog.plugin_manage.hook_constants import ARTICLE_CONTENT_HOOK_NAME
class ArticleCopyrightPlugin(BasePlugin):
@ -11,7 +12,7 @@ class ArticleCopyrightPlugin(BasePlugin):
# 2. 实现 register_hooks 方法,专门用于注册钩子
def register_hooks(self):
# 在这里将插件的方法注册到指定的钩子上
hooks.register('the_content', self.add_copyright_to_content)
hooks.register(ARTICLE_CONTENT_HOOK_NAME, self.add_copyright_to_content)
def add_copyright_to_content(self, content, *args, **kwargs):
"""
@ -22,7 +23,7 @@ class ArticleCopyrightPlugin(BasePlugin):
if not article:
return content
copyright_info = f"\n<hr><p>本文由 {article.author.nickname} 原创,转载请注明出处。</p>"
copyright_info = f"\n<hr><p>本文由 {article.author.username} 原创,转载请注明出处。</p>"
return content + copyright_info

@ -2,6 +2,7 @@ import re
from urllib.parse import urlparse
from djangoblog.plugin_manage.base_plugin import BasePlugin
from djangoblog.plugin_manage import hooks
from djangoblog.plugin_manage.hook_constants import ARTICLE_CONTENT_HOOK_NAME
class ExternalLinksPlugin(BasePlugin):
@ -11,7 +12,7 @@ class ExternalLinksPlugin(BasePlugin):
PLUGIN_AUTHOR = 'liangliangyy'
def register_hooks(self):
hooks.register('the_content', self.process_external_links)
hooks.register(ARTICLE_CONTENT_HOOK_NAME, self.process_external_links)
def process_external_links(self, content, *args, **kwargs):
from djangoblog.utils import get_current_site

@ -1,6 +1,8 @@
import math
import re
from djangoblog.plugin_manage.base_plugin import BasePlugin
from djangoblog.plugin_manage import hooks
from djangoblog.plugin_manage.hook_constants import ARTICLE_CONTENT_HOOK_NAME
class ReadingTimePlugin(BasePlugin):
@ -10,7 +12,7 @@ class ReadingTimePlugin(BasePlugin):
PLUGIN_AUTHOR = 'liangliangyy'
def register_hooks(self):
hooks.register('the_content', self.add_reading_time)
hooks.register(ARTICLE_CONTENT_HOOK_NAME, self.add_reading_time)
def add_reading_time(self, content, *args, **kwargs):
"""
@ -26,8 +28,8 @@ class ReadingTimePlugin(BasePlugin):
word_count = len(words)
# 按平均每分钟200字的速度计算
reading_speed = 200
reading_minutes = round(word_count / reading_speed)
reading_speed = 200
reading_minutes = math.ceil(word_count / reading_speed)
# 如果阅读时间少于1分钟则显示为1分钟
if reading_minutes < 1:

Loading…
Cancel
Save