Skip to content

Commit

Permalink
web: Add custom backlink support
Browse files Browse the repository at this point in the history
  • Loading branch information
laymonage committed Jul 30, 2022
1 parent 5e5b4e0 commit 1a837fa
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions web/src/giscus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,11 @@ export class GiscusWidget extends LitElement {
this.updateConfig();
}

private _getOgMetaContent(property: string) {
const element = document.querySelector(
`meta[property='og:${property}'],meta[name='${property}']`
) as HTMLMetaElement;
private getMetaContent(property: string, og = false) {
const ogSelector = og ? `meta[property='og:${property}'],` : '';
const element = document.querySelector<HTMLMetaElement>(
ogSelector + `meta[name='${property}']`
);

return element ? element.content : '';
}
Expand All @@ -265,7 +266,7 @@ export class GiscusWidget extends LitElement {
case 'title':
return document.title;
case 'og:title':
return this._getOgMetaContent('title');
return this.getMetaContent('title', true);
case 'specific':
return this.term || '';
case 'number':
Expand All @@ -283,11 +284,12 @@ export class GiscusWidget extends LitElement {
}

private getIframeSrc() {
const url = this._getCleanedUrl();
const url = this._getCleanedUrl().toString();

const origin = `${url}${this.id ? '#' + this.id : ''}`;

const description = this._getOgMetaContent('description');
const description = this.getMetaContent('description', true);
const backLink = this.getMetaContent('giscus:backlink') || url;

const params: Record<string, string> = {
origin,
Expand All @@ -304,6 +306,7 @@ export class GiscusWidget extends LitElement {
inputPosition: this.inputPosition,
theme: this.theme,
description,
backLink,
};

const locale = this.lang ? `/${this.lang}` : '';
Expand Down Expand Up @@ -395,6 +398,7 @@ interface ISetConfigMessage {
categoryId?: string;
term?: string;
description?: string;
backLink?: string;
number?: number;
strict?: boolean;
reactionsEnabled?: boolean;
Expand Down

1 comment on commit 1a837fa

@vercel
Copy link

@vercel vercel bot commented on 1a837fa Jul 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

giscus-component – ./

giscus-component-git-main-giscus.vercel.app
giscus-component-giscus.vercel.app
giscus-component.vercel.app

Please sign in to comment.