Biblioteca de queries
Biblioteca de queriesExibir todos os links em todos os posts

Exibir todos os links em todos os posts

Esta query exibe todos os links adicionados em todos os posts.

Ela encontra todas as strings <a href="(...)">(...)</a> em todos os posts e as lista na resposta no formato { href: (...), text: (...) }.

query GetPostLinks {
  posts(pagination: { limit: -1 }) {
    id
    title
    
    # Get the post content, and identify the links
    rawContent
      @remove
    adaptedRawContent: _strRegexReplace(
      searchRegex: "#<a.*(?=href=\"([^\"]*)\")[^>]*>([^<]*)<\/a>#i",
      replaceWith: "*****|||||$1|||||$2*****",
      in: $__rawContent
    )
      @remove
    
    # Extract the links into an object { href: ..., text: ...}
    links: _strSplit(
      string: $__adaptedRawContent,
      separator: "*****"
    )
      @underEachArrayItem(
        passValueOnwardsAs: "entry"
        affectDirectivesUnderPos: [1, 2, 3]
      )
        @applyField(
          name: "_strStartsWith"
          arguments: {
            search: "|||||"
            in: $entry
          }
          passOnwardsAs: "isMatch"
        )
        @applyField(
          name: "_not"
          arguments: {
            value: $isMatch
          }
          passOnwardsAs: "isNotMatch"
        )
        @if(
          condition: $isNotMatch
        )
          @setNull
    
      @arrayFilter
    
      @underEachArrayItem(
        passValueOnwardsAs: "match"
        affectDirectivesUnderPos: [1, 2, 3, 4]
      )
        @applyField(
          name: "_strSplit"
          arguments: {
            separator: "|||||"
            string: $match
          }
          passOnwardsAs: "matchSplit"
        )
        @applyField(
          name: "_arrayItem"
          arguments: {
            array: $matchSplit
            position: 1
          }
          passOnwardsAs: "matchHref"
        )
        @applyField(
          name: "_arrayItem"
          arguments: {
            array: $matchSplit
            position: 2
          }
          passOnwardsAs: "matchText"
        )
        @applyField(
          name: "_echo"
          arguments: {
            value: {
              href: $matchHref
              text: $matchText
            }
          }
          setResultInResponse: true
        )
  }
}