Query 库为文章添加缺失链接
为文章添加缺失链接
此 query 通过正则表达式搜索和替换,为文章的 HTML 内容中缺失的链接进行补充。
所有未被锚点标签包裹的 URL(例如以下内容):
<p>Visit my website: https://mysite.com.</p>...将被加上对应的 <a> 标签(同时从文本中移除域名,并添加在新窗口中打开的 target 属性),变为:
<p>Visit my website: <a href="https://mysite.com" target="_blank">mysite.com</a>.</p>query GetPostData($postId: ID!) {
post(by: { id: $postId }, status: any) {
id
rawContent
adaptedRawContent: _strRegexReplace(
searchRegex: "#\\s+((https?)://(\\S*?\\.\\S*?))([\\s)\\[\\]{},;\"\\':<]|\\.\\s|$)#i"
replaceWith: "<a href=\"$1\" target=\"_blank\">$3</a>$4"
in: $__rawContent
)
@export(as: "adaptedRawContent")
}
}
mutation AddMissingLinksInPost($postId: ID!)
@depends(on: "GetPostData")
{
updatePost(input: {
id: $postId,
contentAs: { html: $adaptedRawContent },
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
title
rawContent
}
}
}