Discourse에서 LinkedIn 공유 버튼의 URL 인코딩 방법

Discourse 호스팅 커뮤니티에서 관리자로서 배지에 LinkedIn 공유 버튼을 추가하려고 합니다.

이를 위해 이 플러그인 아울렛을 사용하려고 합니다. 테마의 head 섹션에 다음 코드를 포함했습니다.

<script type='text/x-handlebars' data-template-name='/connectors/badge-contents-top/linkedin-button'>
  <a href="https://www.linkedin.com/profile/add?certUrl=https://community.my_community.io{{@outletArgs.url}}" class="linkedin-share-button">
    <img src="https://download.linkedin.com/desktop/add2profile/buttons/en_US.png " alt="LinkedIn Add to Profile button">
  </a>
</script>

href 속성에 주목해 주세요: https://www.linkedin.com/profile/add?certUrl=https://community.my_community.io/{{@outletArgs.url}}.

@outletArgs.url 값은 https://community.my_community.io/badges/<badge_id>/<badge_name>?username=<username>과 같은 형태입니다. 저는 username=<username> 부분에 집중하고 싶습니다.

URL은 인코딩되지 않았고, 등호(=)가 그대로 전달되므로 브라우저가 https://www.linkedin.com/profile/add?certUrl=https://community.my_community.io/https://community.my_community.io/badges/<badge_id>/<badge_name>?username=<username>을 처리할 때 =<username>을 무시합니다.

여기서 URL을 어떻게 인코딩해야 하나요?

I handled this by using Discourse Theme CLI in the following way:

  • Created a new theme and made it a component (ran discourse_theme new and followed the prompts).

  • In javascripts/discourse/connectors/badge-contents-top created a JavaScript file with content not far from. . .

    import Component from "@glimmer/component";
    import { inject as service } from "@ember/service";
    
    export default class LinkedinButton extends Component {
      @service siteSettings;
    
      get encodedUrl() {
        return encodeURIComponent(this.args.outletArgs.url);
      }
    }
    
  • In the same directory as above, created a Handlebars file that roughly contains the inner HTML from the script tag in the question.

    • I gave this and the previous file the same name, other than their termination, that is, for what it’s worth.

I don’t see an application of this? Is it redundant?

Yes! Thank you for catching that.