Queries Library
Queries LibraryAdd missing links in post

Add missing links in post

This query does a regex search and replace to add missing links in the post's HTML content.

All URLs which are not surrounded by an anchor tag, such as:

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

...are added the correspondig <a> tag around them (while also removing the domain from the text, and adding a target to open in a new window), becoming:

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