-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
render external resource OG #23
Merged
Merged
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
fd70e65
wip: external og
takurinton f0ce687
...
takurinton b16f108
...
takurinton 23a0695
...
takurinton 73211f8
...
takurinton eba0a53
...
takurinton f916f94
will delete this file before merging
takurinton 2f20d07
...
takurinton 46d18f3
...
takurinton 672d2be
...
takurinton 757a260
add: renderer.ts
takurinton 6f49e88
replace: renderer
takurinton 8ed3417
...
takurinton 5a3b156
fix: og render is done with CSR
takurinton 701f09a
Merge branch 'main' into add-external-link
takurinton 7af1694
update @types/node
takurinton 9089c5a
downgrade preact version
takurinton c0ad5d7
remove: test post
takurinton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,12 @@ | ||
--- | ||
id: 112 | ||
title: og | ||
description: og について | ||
created_at: 2022-10-11 | ||
--- | ||
|
||
hogehoge | ||
|
||
@og[https://blog.takurinton.dev/post/111] | ||
|
||
@twitter[https://twitter.com/takurinton/status/1579390918603706368] |
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,114 @@ | ||
const getMetaTags = (html, link) => { | ||
const description = html.getElementsByName("description")[0]; | ||
|
||
const ogImage = html.querySelector('meta[property="og:image"]'); | ||
const ogImageContent = ogImage ? ogImage.content : { content: "" }; | ||
|
||
const domain = link.match(/^https?:\/{2,}(.*?)(?:\/|\?|#|$)/)[1]; | ||
let image; | ||
if (ogImage === undefined) { | ||
image = ""; | ||
} else if (/^https?:\/\//.test(ogImageContent)) { | ||
const file = ogImageContent; | ||
const fileLink = file.match(/^https?:\/{2,}(.*?)(?:\/|\?|#|$)/); | ||
|
||
if (fileLink === null) image = `https://${domain}${file.slice(7)}`; | ||
else if (fileLink[1] !== domain) { | ||
const filePathSplit = file.split("/")[3]; | ||
image = `https://${fileLink[1]}/${filePathSplit}`; | ||
} | ||
} else { | ||
const file = ogImageContent; | ||
const fileLink = file.match(/^https?:\/{2,}(.*?)(?:\/|\?|#|$)/); | ||
if (fileLink === null) image = `https://${domain}${file.slice(7)}`; | ||
else { | ||
const filePathSplit = file.split("/").slice(3).join("/"); | ||
image = `https://${domain}/${filePathSplit}`; | ||
} | ||
} | ||
|
||
return { | ||
title: html.title, | ||
description: description === undefined ? "" : description.content, | ||
image: image ?? "", | ||
}; | ||
}; | ||
|
||
const fetchExternalHtml = async (url) => { | ||
const res = await fetch(url); | ||
const html = await res.text(); | ||
return html; | ||
}; | ||
|
||
export const setHtml = async (url, id) => { | ||
const htmlString = await fetchExternalHtml(url); | ||
const html = new DOMParser().parseFromString(htmlString, "text/html"); | ||
const { title, description, image } = getMetaTags(html, url); | ||
|
||
if (typeof window !== "undefined") { | ||
const el = document.getElementById(id); | ||
if (el === null) return; | ||
|
||
el.innerHTML = ` | ||
<div class="og"> | ||
<a href="${url}" target="_blank" > | ||
<div class="left"> | ||
<img src="${image}" alt="${title}" /> | ||
</div> | ||
<div class="right"> | ||
<h1>${title}</h1> | ||
<p class="description">${description}</p> | ||
<p class="link">${url}</p> | ||
</div> | ||
</a> | ||
</div>`; | ||
|
||
const style = Object.assign(document.createElement("style"), { | ||
innerHTML: ` | ||
.og > a { | ||
border: 1px gray solid; | ||
border-radius: 5px; | ||
width: 80%; | ||
padding: 10px; | ||
display: flex; | ||
text-decoration: none; | ||
color: #222222; | ||
} | ||
.left { | ||
height: 100px; | ||
width: 100px; | ||
text-align: center; | ||
padding-right: 40px; | ||
} | ||
.left > img { | ||
height: 100px; | ||
width: 100px; | ||
} | ||
.right { | ||
display: block; | ||
overflow: hidden; | ||
} | ||
.right > h1, | ||
.right > p, | ||
.right > a { | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
white-space: nowrap; | ||
text-overflow: ellipsis; | ||
} | ||
.right > h1 { | ||
height: 50px; | ||
margin: 0; | ||
} | ||
.right > p { | ||
margin: 0; | ||
} | ||
.link { | ||
color: gray; | ||
} | ||
`, | ||
}); | ||
|
||
document.head.appendChild(style); | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,137 +1,8 @@ | ||
import { marked } from "marked"; | ||
import { renderer } from "./renderer"; | ||
import { plugin } from "./plugin"; | ||
|
||
const twitter = { | ||
name: "twitter", | ||
level: "block", | ||
start(src) { | ||
return src.match(/^@twitter\[.*\]$/)?.index; | ||
}, | ||
// eslint-disable-next-line no-unused-vars | ||
tokenizer(src, tokens) { | ||
const rule = /^@twitter\[(.*)\]/; | ||
const match = rule.exec(src); | ||
if (match !== null) { | ||
const token = { | ||
type: "twitter", | ||
raw: match[0], | ||
text: match[1], | ||
id: match[1].split("/").pop(), | ||
tokens: [], | ||
}; | ||
// @ts-ignore | ||
this.lexer.inline(token.text, token.tokens); | ||
return token; | ||
} | ||
}, | ||
renderer(token) { | ||
return `<blockquote class="twitter-tweet" id="${token.id}"></blockquote>`; | ||
}, | ||
}; | ||
|
||
// const og = { | ||
// name: "og", | ||
// level: "block", | ||
// start(src) { | ||
// return src.match(/^@og/)?.index; | ||
// }, | ||
// tokenizer(src, tokens) { | ||
// const rule = /^@og\[.*\]/; | ||
// const match = rule.exec(src); | ||
|
||
// const link = /^([^'"]*[^\s])\s+(['"])(.*)\2/.exec(src); | ||
// // console.log(link); | ||
|
||
// if (match !== null) { | ||
// const token = { | ||
// type: "og", | ||
// raw: match[0], | ||
// text: match[0].trim(), | ||
// tokens: [], | ||
// }; | ||
// this.lexer.inline(token.text, token.tokens); | ||
// return token; | ||
// } | ||
// }, | ||
// renderer(token) { | ||
// return `hoge`; | ||
// }, | ||
// }; | ||
|
||
const renderer = { | ||
twitter(type, raw, text) { | ||
const tweetId = text.split("/").pop(); | ||
return ` | ||
<blockquote class="twitter-tweet" id="${tweetId}"></blockquote> | ||
`; | ||
}, | ||
heading(text, level) { | ||
if (level === 1) { | ||
return `<h${level} class="h${level}" style="border-bottom: 2px solid #ff69b4; padding-bottom: 5px">${text}</h${level}>`; | ||
} | ||
return `<h${level} class="h${level}">${text}</h${level}>`; | ||
}, | ||
table(header, body) { | ||
return ` | ||
<table class="table" border="1" width="100%"> | ||
${header} | ||
${body} | ||
</table> | ||
`; | ||
}, | ||
list(body: string, ordered: boolean, start: number) { | ||
if (ordered) { | ||
return ` | ||
<ul style='padding-left: ${start}'> | ||
${body} | ||
</ul> | ||
`; | ||
} | ||
return ` | ||
<ul> | ||
${body} | ||
</ul> | ||
`; | ||
}, | ||
image(href, title, text) { | ||
return ` | ||
<img src=${href} alt=${text} class="content-img ${title ?? ""}" /> | ||
`; | ||
}, | ||
paragraph(text) { | ||
return ` | ||
<p class="p">${text}</p> | ||
`; | ||
}, | ||
link(href, title, text) { | ||
// const isTwitterLink = /^https:\/\/twitter\.com\/\w+\/status\/\d+/.test( | ||
// href | ||
// ); | ||
|
||
// if (isTwitterLink) { | ||
// const tweetId = href.split("/").pop(); | ||
// return ` | ||
// <blockquote class="twitter-tweet" id="${tweetId}"></blockquote> | ||
// `; | ||
// } | ||
|
||
return ` | ||
<a href="${href}" target="_blank" class=${title}>${text}</a> | ||
`; | ||
}, | ||
blockquote(quote) { | ||
return `<blockquote style="border-left:3px solid gray;margin:0 0 0 10px;padding-left:20px;color:gray;">${quote}</blockquote>`; | ||
}, | ||
}; | ||
|
||
// TODO: rintonmd | ||
// https://github.com/takurinton/rintonmd | ||
export const markdown = (md: string): string => { | ||
marked.use({ | ||
extensions: [ | ||
twitter, | ||
// og | ||
], | ||
}); | ||
marked.use({ renderer }); | ||
return marked.parse(md); | ||
}; | ||
export async function markdown(md: string) { | ||
marked.use({ renderer, ...plugin }); | ||
return await marked.parse(md); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
preact is broken since 10.11.1 due to SignalLike changes...
I can't think of a good way to do it, so downgrade once and use it
preactjs/preact#3747