Query 库从 URL 导入 HTML 并在 WordPress 中创建新文章
从 URL 导入 HTML 并在 WordPress 中创建新文章
此 query 将指定 URL 的 HTML 页面作为新文章导入 WordPress。
对于每个 URL,它从元数据中的 <title>...</title> 获取标题,从 <body>...</body> 获取内容,也可以使用 $contentMatchInnerRegex 变量自定义为某个特定的内部 HTML 元素。
使用 $contentMatchInnerRegex,可以指定要捕获 <body> HTML 中的哪个特定子部分。
例如,如果需要从以下内容中提取内容:
<article class="main">...</article>...可以使用以下方式捕获:
{
"contentMatchInnerRegex": ".*?<\\s*?article\\b[^>]*>(.*?)<\\/article\\b[^>]*>.*?"
}query GenerateURLInputs(
$urls: [URL!]!
$contentMatchInnerRegex: String! = "(.*?)"
) {
urlInputs: _echo(value: $urls)
@underEachArrayItem(
passValueOnwardsAs: "url"
)
@applyField(
name: "_echo",
arguments: {
value: {
url: $url,
method: GET
}
},
setResultInResponse: true
)
@export(as: "urlInputs")
@remove
contentMatchRegex: _sprintf(
string: "/(?:<!DOCTYPE html>)?<\\s*?html\\b[^>]*>.*?<\\s*?body\\b[^>]*>%s<\\/body\\b[^>]*>.*?<\\/html\\b[^>]*>/sim",
values: [$contentMatchInnerRegex]
)
@export(as: "contentMatchRegex")
}
query RequestPages
@depends(on: "GenerateURLInputs")
{
urlContents: _sendHTTPRequests(inputs: $urlInputs, async: false) {
statusCode
body
@remove
title: _strRegexReplace(
searchRegex: "/(?:<!DOCTYPE html>)?<\\s*?html\\b[^>]*>.*?<head\\b[^>]*>.*?<\\s*?title\\b[^>]*>(.*?)<\\/title\\b[^>]*>.*?<\\/head\\b[^>]*>(.*?)<\\/html\\b[^>]*>/sim"
replaceWith: "$1"
in: $__body
)
content: _strRegexReplace(
searchRegex: $contentMatchRegex
replaceWith: "$1"
in: $__body
)
createPostInput: _echo(value: {
status: publish,
title: $__title
contentAs: {
html: $__content
}
})
@export(as: "createPostInputs", type: LIST)
}
}
mutation CreatePostsFromURLs
@depends(on: "RequestPages")
{
createPosts(inputs: $createPostInputs) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
status
title
content
}
}
}Next