我想做什么
我正在构建一个 Jekyll ruby 插件,它将用链接到同名帖子 URL 的超链接替换帖子副本文本内容中任何单词的第一次出现。
我遇到的问题
我已经让它工作,但我无法找出process_words
方法中的两个问题:
- 如何在帖子的主要内容副本文本中只搜索帖子标题,而不是帖子或目录之前的元标记(也是在主要帖子副本文本之前生成的(? 我无法让它与Nokigiri一起使用,尽管这似乎是这里的首选工具。
- 如果帖子的URL不在
post.data['url']
,它在哪里? - 另外,有没有更有效、更清洁的方法呢?
当前代码有效,但会替换第一个匹配项,即使它是 HTML 属性的值,如锚点或元标记。
示例结果
我们有一个包含 3 篇文章的博客:
- 爱好
- 食物
- 自行车
而在"爱好"帖子正文中,我们有一个句子,每个单词都是第一次出现在帖子中,如下所示:
I love mountain biking and bicycles in general.
插件将处理该句子并将其输出为:
I love mountain biking and <a href="https://example.com/link/to/bicycles/">bicycles</a> in general.
我当前的代码(已更新 1(
# _plugins/hyperlink_first_word_occurance.rb
require "jekyll"
require 'uri'
module Jekyll
# Replace the first occurance of each post title in the content with the post's title hyperlink
module HyperlinkFirstWordOccurance
POST_CONTENT_CLASS = "page__content"
BODY_START_TAG = "<body"
ASIDE_START_TAG = "<aside"
OPENING_BODY_TAG_REGEX = %r!<body(.*)>s*!
CLOSING_ASIDE_TAG_REGEX = %r!</aside(.*)>s*!
class << self
# Public: Processes the content and updates the
# first occurance of each word that also has a post
# of the same title, into a hyperlink.
#
# content - the document or page to be processes.
def process(content)
@title = content.data['title']
@posts = content.site.posts
content.output = if content.output.include? BODY_START_TAG
process_html(content)
else
process_words(content.output)
end
end
# Public: Determines if the content should be processed.
#
# doc - the document being processes.
def processable?(doc)
(doc.is_a?(Jekyll::Page) || doc.write?) &&
doc.output_ext == ".html" || (doc.permalink&.end_with?("/"))
end
private
# Private: Processes html content which has a body opening tag.
#
# content - html to be processes.
def process_html(content)
content.output = if content.output.include? ASIDE_START_TAG
head, opener, tail = content.output.partition(CLOSING_ASIDE_TAG_REGEX)
else
head, opener, tail = content.output.partition(POST_CONTENT_CLASS)
end
body_content, *rest = tail.partition("</body>")
processed_markup = process_words(body_content)
content.output = String.new(head) << opener << processed_markup << rest.join
end
# Private: Processes each word of the content and makes
# the first occurance of each word that also has a post
# of the same title, into a hyperlink.
#
# html = the html which includes all the content.
def process_words(html)
page_content = html
@posts.docs.each do |post|
post_title = post.data['title'] || post.name
post_title_lowercase = post_title.downcase
if post_title != @title
if page_content.include?(" " + post_title_lowercase + " ") ||
page_content.include?(post_title_lowercase + " ") ||
page_content.include?(post_title_lowercase + ",") ||
page_content.include?(post_title_lowercase + ".")
page_content = page_content.sub(post_title_lowercase, "<a href="#{ post.url }">#{ post_title.downcase }</a>")
elsif page_content.include?(" " + post_title + " ") ||
page_content.include?(post_title + " ") ||
page_content.include?(post_title + ",") ||
page_content.include?(post_title + ".")
page_content = page_content.sub(post_title, "<a href="#{ post.data['url'] }">#{ post_title }</a>")
end
end
end
page_content
end
end
end
end
Jekyll::Hooks.register %i[posts pages], :post_render do |doc|
# code to call after Jekyll renders a post
Jekyll::HyperlinkFirstWordOccurance.process(doc) if Jekyll::HyperlinkFirstWordOccurance.processable?(doc)
end
更新 1
用@Keith Mifsud的建议更新了我的代码。 现在使用侧边栏的aside
元素或page__content
类来选择要处理的正文内容。
还改进了检查和替换正确的术语。
PS:我从我的插件开始的代码库示例是@Keith Mifsud 的 jekyll-target-blank 插件
这段代码看起来很熟悉:)我建议您查看 Rspecs 测试文件以测试您的问题:https://github.com/keithmifsud/jekyll-target-blank
我会尝试回答你的问题,对不起,在撰写本文时我无法自己测试这些问题。
如何在帖子的主要内容副本文本中只搜索帖子标题,而不是帖子或目录之前的元标记(也是在主要帖子副本文本之前生成的(?我无法让它与Nokigiri一起使用,尽管这似乎是这里的首选工具。
您的要求是:
1( 忽略<body></body>
标签之外的内容。
这似乎已经在process_html()
方法中实现。此方法声明body_content
的唯一进程,它应该按原样工作。你有测试吗?您如何调试它?相同的字符串拆分在我的插件中有效。即仅处理正文内的内容。
2( 忽略目录 (TOC( 中的内容。 我建议您通过进一步拆分body_content
变量来扩展process_html()
方法。在目录的开始和结束标记之间搜索内容(通过 id、css 类等(并将其排除,然后将其添加回字符串之前或之后的位置process_words
。
3( 是否使用诺基亚插件? 这个插件非常适合解析 html。我认为您正在解析字符串,然后创建 html。所以香草Ruby和URI插件应该就足够了。如果需要,您仍然可以使用它,但它不会比在 ruby 中拆分字符串更快。
如果帖子的网址不在post.data['url'],它在哪里?
我认为您应该有一种方法来获取所有帖子标题,然后将"单词"与数组匹配。您可以从文档本身获取所有帖子集合doc.site.posts
并且每个帖子都返回标题。process_words()
方法可以检查每个工作,以查看它是否与数组中的项目匹配。但是,如果标题由多个单词组成怎么办?
另外,有没有更有效、更清洁的方法呢?
目前为止,一切都好。我将从修复问题开始,然后重构速度和编码标准。
我再次建议您使用测试来帮助您解决此问题。
让我知道我是否可以提供更多帮助:)