Thư viện Queries
Thư viện QueriesThêm liên kết còn thiếu trong bài viết

Thêm liên kết còn thiếu trong bài viết

Query này thực hiện tìm kiếm và thay thế bằng regex để thêm các liên kết còn thiếu trong nội dung HTML của bài viết.

Tất cả các URL không được bao quanh bởi thẻ anchor, chẳng hạn như:

<p>Visit my website: https://mysite.com.</p>

...sẽ được thêm thẻ <a> tương ứng xung quanh chúng (đồng thời xóa tên miền khỏi văn bản và thêm target để mở trong cửa sổ mới), trở thành:

<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
    }
  }
}