Schema 教程第13课:修改(并重新保存)文章中所有 Image 块的图片 URL
第13课:修改(并重新保存)文章中所有 Image 块的图片 URL
在上一个教程课中,我们了解到可以遍历文章内容中(Gutenberg)块的内部结构,并提取所需项目。
接下来,让我们探索如何转换这些项目,并将其重新保存到文章中。
本教程课的 Query 会修改文章中 core/image 块内的图片 URL:
- 将
mysite.com替换为cdn.mysite.com(以便通过 CDN 分发这些资源) - 将
.jpg替换为.avif
转换(并重新保存)文章中所有 core/image 块图片 URL 的 GraphQL Query
Mutation updatePost 接收文章的 HTML 内容。因此,我们需要:
- 获取文章的
rawContent - 对该 HTML 代码应用转换,将原始 URL 替换为转换后的 URL
- 保存修改后的内容
下面的 GraphQL Query 通过指令 @strRegexReplaceMultiple 对 HTML 代码执行转换,每个正则表达式模式都根据块的内部 HTML 内容动态生成(在本例中,目标为 core/image 块 HTML 代码中的 src 元素)。由于生成的正则表达式模式可能包含正则表达式特殊字符(例如 .、+、( 等),而替换内容可能包含正则表达式替换变量(例如 $1),因此必须对它们进行转义。
query InitializeEmptyVariables {
emptyArray: _echo(value: [])
@export(as: "coreImageURLItems")
@export(as: "coreImageURLReplacementsFrom")
@export(as: "coreImageURLReplacementsTo")
@remove
}
# Extract all the image URLs from the `core/image` blocks, and export them under `$coreImageURLItems`
query FetchData($postID: ID!)
@configureWarningsOnExportingDuplicateVariable(enabled: false)
@depends(on: "InitializeEmptyVariables")
{
post(by: { id: $postID } ) {
rawContent
@export(as: "rawContent")
coreImage: blockFlattenedDataItems(
filterBy: { include: "core/image" }
)
@underEachArrayItem
@underJSONObjectProperty(
by: { key: "attributes" }
)
@underJSONObjectProperty(
by: { key: "url" }
failIfNonExistingKeyOrPath: false
)
@export(
as: "coreImageURLItems"
)
}
}
# Convert the URLs and place the results under `$transformations`
query TransformData
@depends(on: "FetchData")
{
transformations: _echo(value: {
coreImageURL: {
from: $coreImageURLItems,
to: $coreImageURLItems,
},
})
@underEachJSONObjectProperty
@underJSONObjectProperty(by: { key: "to" })
@underEachArrayItem
@strRegexReplace(
searchRegex: "#^https?://mysite.com/(.*)\\.jpg$#",
replaceWith: "https://cdn.mysite.com/$1.avif"
)
@export(as: "transformations")
}
# Escape the regex patterns and their replacements
query EscapeRegexStrings
@depends(on: "TransformData")
{
escapedRegexStrings: _echo(value: $transformations)
@underEachJSONObjectProperty
@underJSONObjectProperty(by: { key: "from" })
@underEachArrayItem
@strQuoteRegex
@underEachJSONObjectProperty
@underJSONObjectProperty(
by: { key: "to" }
affectDirectivesUnderPos: [1, 3],
)
@underEachArrayItem
@strRegexReplace(
searchRegex: "#\\$(\\d+)#",
replaceWith: "\\\\\\$1"
)
@underEachArrayItem(
passValueOnwardsAs: "value"
)
@applyField(
name: "_sprintf",
arguments: {
string: "$1%s$2",
values: [$value]
},
setResultInResponse: true
)
@export(as: "escapedRegexTransformations")
}
# Generate the regex patterns, and assign them to `$coreImageURLReplacementsFrom`
query CreateRegexReplacements
@depends(on: "EscapeRegexStrings")
{
regexReplacements: _echo(value: $escapedRegexTransformations)
@underJSONObjectProperty(
by: { key: "coreImageURL" }
affectDirectivesUnderPos: [1, 5]
)
@underJSONObjectProperty(
by: { key: "from" }
affectDirectivesUnderPos: [1, 3],
)
@underEachArrayItem(
passValueOnwardsAs: "value"
)
@applyField(
name: "_sprintf",
arguments: {
string: "#(<!-- wp:image .*?-->\\n?.*<img .*?src=\\\")%s(\\\".*>.*\\n?<!-- /wp:image -->)#",
values: [$value]
},
setResultInResponse: true
)
@export(
as: "coreImageURLReplacementsFrom",
)
@underJSONObjectProperty(
by: { key: "to" }
)
@export(
as: "coreImageURLReplacementsTo",
)
}
# Execute the regex search and replace, export the results under `$transformedRawContent`
query ExecuteRegexReplacements
@depends(on: "CreateRegexReplacements")
{
transformedRawContent: _echo(value: $rawContent)
@strRegexReplaceMultiple(
limit: 1,
searchRegex: $coreImageURLReplacementsFrom,
replaceWith: $coreImageURLReplacementsTo
)
@export(as: "transformedRawContent")
}
# Execute the mutation to update the post
mutation ModifyAndUpdatePost($postID: ID!)
@depends(on: "ExecuteRegexReplacements")
{
updatePost(input: {
id: $postID,
contentAs: {
html: $transformedRawContent
}
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
title
rawContent
}
}
}响应如下:
{
"data": {
"post": {
"rawContent": "<!-- wp:paragraph -->\n<p>This is a paragraph block.</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:heading -->\n<h2 class=\"wp-block-heading\">Image Block (Standard)</h2>\n<!-- /wp:heading -->\n\n<!-- wp:image {\"sizeSlug\":\"large\"} -->\n<figure class=\"wp-block-image size-large\"><img src=\"http://mysite.com/fs_img/mysite/2008/themes_photo.jpg\" alt=\"\"/></figure>\n<!-- /wp:image -->\n\n<!-- wp:image {\"sizeSlug\":\"large\"} -->\n<figure class=\"wp-block-image size-large\"><img src=\"http://mysite.com/fs_img/mysite/2007/email_photo.jpg\" alt=\"\"/></figure>\n<!-- /wp:image -->",
"coreImage": [
{
"name": "core/image",
"attributes": {
"sizeSlug": "large",
"url": "http://mysite.com/fs_img/mysite/2008/themes_photo.jpg",
"alt": ""
}
},
{
"name": "core/image",
"attributes": {
"sizeSlug": "large",
"url": "http://mysite.com/fs_img/mysite/2007/email_photo.jpg",
"alt": ""
}
}
]
},
"transformations": {
"coreImageURL": {
"from": [
"http://mysite.com/fs_img/mysite/2008/themes_photo.jpg",
"http://mysite.com/fs_img/mysite/2007/email_photo.jpg"
],
"to": [
"https://cdn.mysite.com/fs_img/mysite/2008/themes_photo.avif",
"https://cdn.mysite.com/fs_img/mysite/2007/email_photo.avif"
]
}
},
"escapedRegexStrings": {
"coreImageURL": {
"from": [
"http://mysite\\.com/fs_img/mysite/2008/themes_photo\\.jpg",
"http://mysite\\.com/fs_img/mysite/2007/email_photo\\.jpg"
],
"to": [
"$1https://cdn.mysite.com/fs_img/mysite/2008/themes_photo.avif$2",
"$1https://cdn.mysite.com/fs_img/mysite/2007/email_photo.avif$2"
]
}
},
"regexReplacements": {
"coreImageURL": {
"from": [
"#(<!-- wp:image .*?-->\\n?.*<img .*?src=\\\")http://mysite\\.com/fs_img/mysite/2008/themes_photo\\.jpg(\\\".*>.*\\n?<!-- /wp:image -->)#",
"#(<!-- wp:image .*?-->\\n?.*<img .*?src=\\\")http://mysite\\.com/fs_img/mysite/2007/email_photo\\.jpg(\\\".*>.*\\n?<!-- /wp:image -->)#"
],
"to": [
"$1https://cdn.mysite.com/fs_img/mysite/2008/themes_photo.avif$2",
"$1https://cdn.mysite.com/fs_img/mysite/2007/email_photo.avif$2"
]
}
},
"transformedRawContent": "<!-- wp:paragraph -->\n<p>This is a paragraph block.</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:heading -->\n<h2 class=\"wp-block-heading\">Image Block (Standard)</h2>\n<!-- /wp:heading -->\n\n<!-- wp:image {\"sizeSlug\":\"large\"} -->\n<figure class=\"wp-block-image size-large\"><img src=\"https://cdn.mysite.com/fs_img/mysite/2008/themes_photo.avif\" alt=\"\"/></figure>\n<!-- /wp:image -->\n\n<!-- wp:image {\"sizeSlug\":\"large\"} -->\n<figure class=\"wp-block-image size-large\"><img src=\"https://cdn.mysite.com/fs_img/mysite/2007/email_photo.avif\" alt=\"\"/></figure>\n<!-- /wp:image -->",
"updatePost": {
"status": "SUCCESS",
"errors": null,
"post": {
"id": 13,
"title": "Released v0.6, check it out",
"rawContent": "<!-- wp:paragraph -->\n<p>This is a paragraph block.</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:heading -->\n<h2 class=\"wp-block-heading\">Image Block (Standard)</h2>\n<!-- /wp:heading -->\n\n<!-- wp:image {\"sizeSlug\":\"large\"} -->\n<figure class=\"wp-block-image size-large\"><img src=\"https://cdn.mysite.com/fs_img/mysite/2008/themes_photo.avif\" alt=\"\"/></figure>\n<!-- /wp:image -->\n\n<!-- wp:image {\"sizeSlug\":\"large\"} -->\n<figure class=\"wp-block-image size-large\"><img src=\"https://cdn.mysite.com/fs_img/mysite/2007/email_photo.avif\" alt=\"\"/></figure>\n<!-- /wp:image -->"
}
}
}
}