쿼리 라이브러리
쿼리 라이브러리게시물에 누락된 링크 추가하기

게시물에 누락된 링크 추가하기

이 쿼리는 정규식 검색 및 바꾸기를 사용하여 게시물의 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
    }
  }
}