From a98530dd6bf87f826b2efe7d8c2b8921ce3b5f78 Mon Sep 17 00:00:00 2001 From: Simon Lecoq <22963968+lowlighter@users.noreply.github.com> Date: Wed, 7 Apr 2021 14:13:56 +0200 Subject: [PATCH] Plugin markdown update (#211) --- source/app/metrics/index.mjs | 2 +- source/plugins/posts/index.mjs | 9 +++-- source/plugins/posts/queries/hashnode.graphql | 3 +- source/plugins/tweets/index.mjs | 14 +++++--- source/templates/markdown/example.md | 20 +++-------- .../templates/markdown/partials/activity.ejs | 9 +++-- source/templates/markdown/partials/posts.ejs | 35 +++++++++++++++++++ source/templates/markdown/partials/rss.ejs | 2 +- source/templates/markdown/partials/topics.ejs | 2 +- source/templates/markdown/partials/tweets.ejs | 21 +++++++++++ 10 files changed, 87 insertions(+), 30 deletions(-) create mode 100644 source/templates/markdown/partials/posts.ejs create mode 100644 source/templates/markdown/partials/tweets.ejs diff --git a/source/app/metrics/index.mjs b/source/app/metrics/index.mjs index 51c9db2d..58a2fccf 100644 --- a/source/app/metrics/index.mjs +++ b/source/app/metrics/index.mjs @@ -68,7 +68,7 @@ console.debug(`metrics/compute/${login} > markdown render`) let source = image try { - let template = q.markdown + let template = `${q.markdown}`.replace(/\n/g, "") if (!/^https:/.test(template)) { const {data:{default_branch:branch, full_name:repo}} = await rest.repos.get({owner:login, repo:q.repo||login}) console.debug(`metrics/compute/${login} > on ${repo} with default branch ${branch}`) diff --git a/source/plugins/posts/index.mjs b/source/plugins/posts/index.mjs index 9d652384..fab4673f 100644 --- a/source/plugins/posts/index.mjs +++ b/source/plugins/posts/index.mjs @@ -12,16 +12,19 @@ //Retrieve posts console.debug(`metrics/compute/${login}/plugins > posts > processing with source ${source}`) let posts = null + let link = null switch (source) { //Dev.to case "dev.to":{ console.debug(`metrics/compute/${login}/plugins > posts > querying api`) - posts = (await imports.axios.get(`https://dev.to/api/articles?username=${user}&state=fresh`)).data.map(({title, description, published_at:date, cover_image:image}) => ({title, description, date, image})) + posts = (await imports.axios.get(`https://dev.to/api/articles?username=${user}&state=fresh`)).data.map(({title, description, published_at:date, cover_image:image, url:link}) => ({title, description, date, image, link})) + link = `https://dev.to/${user}` break } //Hashnode case "hashnode":{ - posts = (await imports.axios.post("https://api.hashnode.com", {query:queries.posts.hashnode({user})}, {headers:{"Content-type":"application/json"}})).data.data.user.publication.posts.map(({title, brief:description, dateAdded:date, coverImage:image}) => ({title, description, date, image})) + posts = (await imports.axios.post("https://api.hashnode.com", {query:queries.posts.hashnode({user})}, {headers:{"Content-type":"application/json"}})).data.data.user.publication.posts.map(({title, brief:description, dateAdded:date, coverImage:image, slug}) => ({title, description, date, image, link:`https://hashnode.com/post/${slug}`})) + link = `https://hashnode.com/@${user}` break } //Unsupported @@ -42,7 +45,7 @@ posts = await Promise.all(posts.map(async({image, ...post}) => ({image:await imports.imgb64(image, {width:144, height:-1}), ...post}))) } //Results - return {source, descriptions, covers, list:posts} + return {source, link, descriptions, covers, list:posts} } //Unhandled error diff --git a/source/plugins/posts/queries/hashnode.graphql b/source/plugins/posts/queries/hashnode.graphql index bb9d6b29..fbc186aa 100644 --- a/source/plugins/posts/queries/hashnode.graphql +++ b/source/plugins/posts/queries/hashnode.graphql @@ -1,7 +1,8 @@ query PostsHashnode { user(username: "$user"){ publication{ - posts(page: 1) { + posts(page: 0) { + slug title brief coverImage diff --git a/source/plugins/tweets/index.mjs b/source/plugins/tweets/index.mjs index 8e8ce2de..dc2c2fa4 100644 --- a/source/plugins/tweets/index.mjs +++ b/source/plugins/tweets/index.mjs @@ -46,11 +46,17 @@ //Medias if (tweet.attachments) tweet.attachments = await Promise.all(tweet.attachments.media_keys.filter(key => medias.get(key)).map(key => medias.get(key)).map(async url => ({image:await imports.imgb64(url, {height:-1, width:450})}))) - else if (linked) { + if (linked) { const {result:{ogImage, ogSiteName:website, ogTitle:title, ogDescription:description}} = await imports.opengraph({url:linked}) const image = await imports.imgb64(ogImage?.url, {height:-1, width:450, fallback:false}) - if (image) - tweet.attachments = [{image, title, description, website}] + if (image) { + if (tweet.attachments) + tweet.attachments.unshift([{image, title, description, website}]) + else + tweet.attachments = [{image, title, description, website}] + } + else + tweet.text = `${tweet.text}\n${linked}` } } else @@ -68,7 +74,7 @@ //Line breaks .replace(/\n/g, "
") //Links - .replace(new RegExp(`${tweet.urls.size ? "" : "noop^"}(${[...tweet.urls.keys()].map(url => `(?:${url})`).join("|")})`, "gi"), (_, url) => `${tweet.urls.get(url)}`), {"&":true}) + .replace(new RegExp(`${tweet.urls.size ? "" : "noop^"}(${[...tweet.urls.keys()].map(url => `(?:${url})`).join("|")})`, "gi"), (_, url) => `${tweet.urls.get(url)}`), {"&":true}) })) //Result diff --git a/source/templates/markdown/example.md b/source/templates/markdown/example.md index b80c8d65..b845185a 100644 --- a/source/templates/markdown/example.md +++ b/source/templates/markdown/example.md @@ -20,28 +20,16 @@ I contributed to `{{ REPOSITORIES_CONTRIBUTED_TO }}` repositories and made `{{ C ## 🧩 Markdown plugins -Most of plugins from SVG templates can be reused directly by including image source in markdown, but some have them have their own **markdown** version which includes hyperlinks. +Most of plugins from SVG templates can be reused directly by including image source in markdown, but some have them have their own **markdown** version which includes hyperlinks and reduce image overhead. -### 📰 Recent activity +See [compatibility matrix](https://github.com/lowlighter/metrics#-templateplugin-compatibily-matrix) for more informations. <%- await include(`partials/activity.ejs`) %> -### ✒️ Recent posts - -*Coming soon* - -### 🗼 Rss feed +<%- await include(`partials/posts.ejs`) %> <%- await include(`partials/rss.ejs`) %> -### 🐤 Latest tweets - -*Coming soon* - -### 🌇 GitHub Skyline 3D calendar - -*Coming soon* - -### 📌 Starred topics +<%- await include(`partials/tweets.ejs`) %> <%- await include(`partials/topics.ejs`) %> diff --git a/source/templates/markdown/partials/activity.ejs b/source/templates/markdown/partials/activity.ejs index 13b6cf79..4f070a6e 100644 --- a/source/templates/markdown/partials/activity.ejs +++ b/source/templates/markdown/partials/activity.ejs @@ -5,7 +5,7 @@ <%_ } else if (plugins.activity.events.length) { _%> <%_ for (const {actor, type, repo, timestamp, ...event} of plugins.activity.events) { _%> <%_ if (type === "comment") { _%> -* 💬 Commented on [#<%= event.number %> <%= event.title %>](https://github.com/<%= repo %>/<%= {issue:"issues", pr:"pulls", commit:"commit"}[event.on] %>/<%= event.number %>) from [<%= repo %>](https://github.com/<%= repo %>) +* 💬 Commented on [#<%= event.number %> <%= event.title %>](https://github.com/<%= repo %>/<%= {issue:"issues", pr:"pull", commit:"commit"}[event.on] %>/<%= event.number %>) from [<%= repo %>](https://github.com/<%= repo %>) <%_ } else if (type === "member") { _%> * 💼 Added [<%= event.user %>](https://github.com/<%= event.user %>) as collaborator in [<%= repo %>](https://github.com/<%= repo %>) <%_ } else if (type === "star") { _%> @@ -22,7 +22,7 @@ <%_ } else if (type === "issue") { _%> * #️⃣ <%- event.action === "opened" ? "Opened" : event.action === "reopened" ? "Reopened" : "Closed" %> [#<%= event.number %> <%= event.title %>](https://github.com/<%= repo %>/issues/<%= event.number %>) in [<%= repo %>](https://github.com/<%= repo %>) <%_ } else if (type === "pr") { _%> -* 🔃 <%- event.action === "opened" ? "Opened" : "Merged" %> [#<%= event.number %> <%= event.title %>](https://github.com/<%= repo %>/pulls/<%= event.number %>) in [<%= repo %>](https://github.com/<%= repo %>) +* 🔃 <%- event.action === "opened" ? "Opened" : "Merged" %> [#<%= event.number %> <%= event.title %>](https://github.com/<%= repo %>/pull/<%= event.number %>) in [<%= repo %>](https://github.com/<%= repo %>) * <%= event.files.changed %> file<%= s(event.files.changed) %> changed `++<%= event.lines.added %> --<%= event.lines.deleted%>` <%_ } else if (type === "wiki") { _%> * 📝 Updated <%= event.pages.length %> wiki page<%= s(event.pages.length) %> in [<%= repo %>](https://github.com/<%= repo %>/wiki) @@ -32,12 +32,15 @@ <%_ } else if (type === "public") { _%> * 🚀 Made [<%= repo %>](https://github.com/<%= repo %>) public <%_ } else if (type === "review") { _%> -* 🔍 Reviewed [#<%= event.number %> <%= event.title %>](https://github.com/<%= repo %>/pulls/<%= event.number %>) in [<%= repo %>](https://github.com/<%= repo %>) +* 🔍 Reviewed [#<%= event.number %> <%= event.title %>](https://github.com/<%= repo %>/pull/<%= event.number %>) in [<%= repo %>](https://github.com/<%= repo %>) <%_ } else if (type === "ref/create") { _%> * ⏺️ Created new <%= event.ref.type %> `<%= event.ref.name %>` in [<%= repo %>](https://github.com/<%= repo %>) <%_ } else if (type === "ref/create") { _%> * 🚮 Deleted <%= event.ref.type %> `<%= event.ref.name %>` from [<%= repo %>](https://github.com/<%= repo %>) <%_ } _%> + <%_ if (plugins.activity.timestamps) { _%> + * *On <%= f.date(timestamp, {timeStyle:"short", dateStyle:"short"}) %>* + <%_ } _%> <%_ } _%> <%_ } else { _%> No recent activity diff --git a/source/templates/markdown/partials/posts.ejs b/source/templates/markdown/partials/posts.ejs new file mode 100644 index 00000000..e3610783 --- /dev/null +++ b/source/templates/markdown/partials/posts.ejs @@ -0,0 +1,35 @@ +<%_ if (plugins.posts) { _%> +**[✒️ Recent posts from <%= plugins.posts?.source %>](<%= plugins.posts?.link %>)** + <%_ if (plugins.posts.error) { _%> +<%= plugins.posts.error.message _%> + <%_ } else if ((plugins.posts.list.length)&&(plugins.posts.descriptions)||(plugins.posts.covers)) { _%> + <%_ for (const {title, date, link, description = "", image} of plugins.posts.list) { _%> + + + <%_ if (plugins.posts.covers) { _%> + + <%_ } _%> + + + + + +
+ + + <%= title.trim() %> +
+ <%= description %> +
+ Published on <%= f.date(new Date(date), {dateStyle:"short"}) %> +
+ <%_ } _%> + <%_ } else if (plugins.posts.list.length) { _%> + <%_ for (const {title, date, link} of plugins.posts.list) { _%> +* [<%= title.trim() %>](<%= link %>) + * *Published on <%= f.date(new Date(date), {dateStyle:"short"}) %>* + <%_ } _%> + <%_ } else { _%> +No recent posts + <%_ } _%> +<%_ } _%> \ No newline at end of file diff --git a/source/templates/markdown/partials/rss.ejs b/source/templates/markdown/partials/rss.ejs index f90f80ad..0f74f497 100644 --- a/source/templates/markdown/partials/rss.ejs +++ b/source/templates/markdown/partials/rss.ejs @@ -5,7 +5,7 @@ <%_ } else if (plugins.rss.feed.length) { _%> <%_ for (const {title, link, date} of plugins.rss.feed) { _%> * [<%= title %>](<%= link %>) - * Published on <%= f.date(new Date(date), {dateStyle:"short"}) %> + * *Published on <%= f.date(new Date(date), {dateStyle:"short"}) %>* <%_ } _%> <%_ } else { _%> Empty RSS feed diff --git a/source/templates/markdown/partials/topics.ejs b/source/templates/markdown/partials/topics.ejs index c4355f39..b16e40aa 100644 --- a/source/templates/markdown/partials/topics.ejs +++ b/source/templates/markdown/partials/topics.ejs @@ -7,6 +7,6 @@ * [<%= name %>](https://github.com/topics/<%= name.toLocaleLowerCase().replace(/ /g, "-") %>) <%_ } _%> <%_ } else { _%> -Empty RSS feed +No starred topics <%_ } _%> <%_ } _%> \ No newline at end of file diff --git a/source/templates/markdown/partials/tweets.ejs b/source/templates/markdown/partials/tweets.ejs new file mode 100644 index 00000000..c0ff3d95 --- /dev/null +++ b/source/templates/markdown/partials/tweets.ejs @@ -0,0 +1,21 @@ +<%_ if (plugins.tweets) { _%> +**[🐤 Latest tweets from @<%= plugins.tweets.username %>](https://twitter.com/<%= plugins.tweets.username %>)** + <%_ if (plugins.tweets.error) { _%> +<%= plugins.tweets.error.message _%> + <%_ } else if (plugins.tweets.list.length) { _%> + <%_ for (const {text, createdAt, attachments} of plugins.tweets.list) { _%> +> <%- text %> +<%_ if (attachments) { _%> + <%_ for (const {image, title, description, website} of attachments) { _%> +> +> <%= title %> + <%_ } _%> +<%_ } _%> +> +> *<%= createdAt %>* + + <%_ } _%> + <%_ } else { _%> +No recent tweets + <%_ } _%> +<%_ } _%> \ No newline at end of file