-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
181 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
"ok": "deno fmt --check && deno lint && deno task check:types && deno task test" | ||
}, | ||
"imports": { | ||
"$fresh/": "https://deno.land/x/fresh@1.6.3/", | ||
"$fresh/": "https://raw.githubusercontent.com/denoland/fresh/844370cadd1ed28fd76f796c2afc1e2411bfc425/", | ||
"tailwindcss": "npm:[email protected]", | ||
"preact": "https://esm.sh/[email protected]", | ||
"preact/": "https://esm.sh/[email protected]/", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// deno-lint-ignore-file | ||
import { useEffect } from "preact/hooks"; | ||
|
||
const DISQUS_INSTANCE: any = "DISQUS"; | ||
const DISQUS_CONFIG: any = "disqus_config"; | ||
const DISQUS_SHORTNAME: any = "disqus_shortname"; | ||
const DISQUS_THREAD: any = "disqus_thread"; | ||
const DISQUS_COMMENT_ELEMENT_ID: any = "dsq-embed-scr"; | ||
const DISQUS_PRELOAD_IFRAME_SELECTOR: any = "iframe[id^=dsq-app]"; | ||
|
||
const DEFER_LOADING_MS = 4000; | ||
|
||
export interface DisqusCommentProps { | ||
title: string; | ||
identifier: string; | ||
url: string; | ||
shortname: string; | ||
|
||
defer?: number; // defer loading (ms) | ||
} | ||
|
||
const EMPTY_DISQUS_CONFIG: any = { | ||
identifier: "", | ||
url: "", | ||
shortname: "", | ||
defer: DEFER_LOADING_MS, | ||
}; | ||
|
||
export function Comment(props: any) { | ||
const disqusConfig = Object.assign({}, EMPTY_DISQUS_CONFIG, props); | ||
|
||
const insertScript = (src: any, id: any, parentElement: any) => { | ||
const script = document.createElement("script"); | ||
script.async = true; | ||
script.src = src; | ||
script.id = id; | ||
parentElement.appendChild(script); | ||
return script; | ||
}; | ||
|
||
const removeScript = (id: any, parentElement: any) => { | ||
const script = document.getElementById(id); | ||
if (script) { | ||
parentElement.removeChild(script); | ||
} | ||
}; | ||
|
||
const removeDisqusThreadElement = () => { | ||
const disqusThread = document.getElementById(DISQUS_THREAD); | ||
if (disqusThread) { | ||
while (disqusThread.hasChildNodes()) { | ||
disqusThread.firstChild && | ||
disqusThread.removeChild(disqusThread.firstChild); | ||
} | ||
} | ||
}; | ||
|
||
const removeDisqusPreloadFrameElement = () => { | ||
const disqusPreloadFrame = document.querySelectorAll( | ||
DISQUS_PRELOAD_IFRAME_SELECTOR, | ||
); | ||
|
||
if (disqusPreloadFrame) { | ||
disqusPreloadFrame.forEach((value, key, parent) => { | ||
value.parentElement?.removeChild(value); | ||
}); | ||
} | ||
}; | ||
|
||
const getDisqusConfig = () => { | ||
return function (this: any) { | ||
this.page.identifier = disqusConfig.identifier; | ||
this.page.url = disqusConfig.url; | ||
this.page.title = props.title; | ||
}; | ||
}; | ||
|
||
const loadInstance = () => { | ||
setTimeout(() => { | ||
if ( | ||
window[DISQUS_INSTANCE] && | ||
document.getElementById(DISQUS_COMMENT_ELEMENT_ID) | ||
) { | ||
(window[DISQUS_INSTANCE] as any).reset({ | ||
reload: true, | ||
config: getDisqusConfig(), | ||
}); | ||
} else { | ||
removeDisqusThreadElement(); | ||
if (props.shortname) { | ||
(window[DISQUS_CONFIG] as any) = getDisqusConfig(); | ||
window[DISQUS_SHORTNAME] = disqusConfig.shortname; | ||
insertScript( | ||
`https://${disqusConfig.shortname}.disqus.com/embed.js`, | ||
DISQUS_COMMENT_ELEMENT_ID, | ||
document.body, | ||
); | ||
} | ||
} | ||
}, disqusConfig.defer); | ||
}; | ||
|
||
const cleanInstance = () => { | ||
removeScript(DISQUS_COMMENT_ELEMENT_ID, document.body); | ||
if (window[DISQUS_INSTANCE]) { | ||
(window[DISQUS_INSTANCE] as any).reset(); | ||
(window[DISQUS_INSTANCE] as any) = undefined; | ||
removeDisqusThreadElement(); | ||
removeDisqusPreloadFrameElement(); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
loadInstance(); | ||
return cleanInstance; | ||
}, [props.identifier]); | ||
|
||
return <div id={DISQUS_THREAD} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,7 @@ const blogOptions: BlogOptions = { | |
} | ||
|
||
configContent = | ||
`import { BlogOptions, blogPlugin } from "https://deno.land/x/[email protected].4/mod.ts"; | ||
`import { BlogOptions, blogPlugin } from "https://deno.land/x/[email protected].6/mod.ts"; | ||
${blogOptions} | ||
${configContent}`; | ||
|
||
|
@@ -89,7 +89,8 @@ async function modifyDenoJson() { | |
return; | ||
} | ||
|
||
denoJson.imports["$fresh/"] = "https://deno.land/x/[email protected]/"; | ||
denoJson.imports["$fresh/"] = | ||
"https://raw.githubusercontent.com/denoland/fresh/844370cadd1ed28fd76f796c2afc1e2411bfc425/"; | ||
|
||
const denoJsonContent = JSON.stringify(denoJson, null, 2) + "\n"; | ||
await Deno.writeTextFile(DENO_JSON_PATH, denoJsonContent); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// credit to https://github.com/haikelfazzani/portfolio for this code | ||
|
||
import { Comment } from "../components/DiscussionEmbed.tsx"; | ||
|
||
type DisqusProp = { | ||
title: string; | ||
identifier: string; | ||
shortname: string; | ||
}; | ||
|
||
export default function Disqus({ title, identifier, shortname }: DisqusProp) { | ||
return ( | ||
<div class="w-100 mt-2"> | ||
<Comment | ||
title={title} | ||
identifier={identifier.toLowerCase().replace(/\s+/g, "-")} | ||
url={`http://localhost:8000/blog/${identifier}`} | ||
shortname={shortname} | ||
/> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
"preview": "deno run -A main.ts" | ||
}, | ||
"imports": { | ||
"$fresh/": "https://deno.land/x/fresh@1.6.3/", | ||
"$fresh/": "https://raw.githubusercontent.com/denoland/fresh/844370cadd1ed28fd76f796c2afc1e2411bfc425/", | ||
"preact": "https://esm.sh/[email protected]", | ||
"preact/": "https://esm.sh/[email protected]/", | ||
"@preact/signals": "https://esm.sh/*@preact/[email protected]", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
"preview": "deno run -A main.ts" | ||
}, | ||
"imports": { | ||
"$fresh/": "https://deno.land/x/fresh@1.6.3/", | ||
"$fresh/": "https://raw.githubusercontent.com/denoland/fresh/844370cadd1ed28fd76f796c2afc1e2411bfc425/", | ||
"preact": "https://esm.sh/[email protected]", | ||
"preact/": "https://esm.sh/[email protected]/", | ||
"@preact/signals": "https://esm.sh/*@preact/[email protected]", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
"preview": "deno run -A main.ts" | ||
}, | ||
"imports": { | ||
"$fresh/": "https://deno.land/x/fresh@1.6.3/", | ||
"$fresh/": "https://raw.githubusercontent.com/denoland/fresh/844370cadd1ed28fd76f796c2afc1e2411bfc425/", | ||
"preact": "https://esm.sh/[email protected]", | ||
"preact/": "https://esm.sh/[email protected]/", | ||
"@preact/signals": "https://esm.sh/*@preact/[email protected]", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
"preview": "deno run -A main.ts" | ||
}, | ||
"imports": { | ||
"$fresh/": "https://deno.land/x/fresh@1.6.3/", | ||
"$fresh/": "https://raw.githubusercontent.com/denoland/fresh/844370cadd1ed28fd76f796c2afc1e2411bfc425/", | ||
"preact": "https://esm.sh/[email protected]", | ||
"preact/": "https://esm.sh/[email protected]/", | ||
"@preact/signals": "https://esm.sh/*@preact/[email protected]", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
"preview": "deno run -A main.ts" | ||
}, | ||
"imports": { | ||
"$fresh/": "https://deno.land/x/fresh@1.6.3/", | ||
"$fresh/": "https://raw.githubusercontent.com/denoland/fresh/844370cadd1ed28fd76f796c2afc1e2411bfc425/", | ||
"preact": "https://esm.sh/[email protected]", | ||
"preact/": "https://esm.sh/[email protected]/", | ||
"@preact/signals": "https://esm.sh/*@preact/[email protected]", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
"preview": "deno run -A main.ts" | ||
}, | ||
"imports": { | ||
"$fresh/": "https://deno.land/x/fresh@1.6.3/", | ||
"$fresh/": "https://raw.githubusercontent.com/denoland/fresh/844370cadd1ed28fd76f796c2afc1e2411bfc425/", | ||
"preact": "https://esm.sh/[email protected]", | ||
"preact/": "https://esm.sh/[email protected]/", | ||
"@preact/signals": "https://esm.sh/*@preact/[email protected]", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
"preview": "deno run -A main.ts" | ||
}, | ||
"imports": { | ||
"$fresh/": "https://deno.land/x/fresh@1.6.3/", | ||
"$fresh/": "https://raw.githubusercontent.com/denoland/fresh/844370cadd1ed28fd76f796c2afc1e2411bfc425/", | ||
"preact": "https://esm.sh/[email protected]", | ||
"preact/": "https://esm.sh/[email protected]/", | ||
"@preact/signals": "https://esm.sh/*@preact/[email protected]", | ||
|