Skip to content

Commit 099a967

Browse files
committed
ajoute une fonction permettant de publier par email
1 parent 54fd6e1 commit 099a967

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

functions/heyworld.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
function string_to_slug(str) {
2+
str = str.replace(/^\s+|\s+$/g, ''); // trim
3+
str = str.toLowerCase();
4+
5+
// remove accents, swap ñ for n, etc
6+
var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
7+
var to = "aaaaeeeeiiiioooouuuunc------";
8+
for (var i=0, l=from.length ; i<l ; i++) {
9+
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
10+
}
11+
12+
str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
13+
.replace(/\s+/g, '-') // collapse whitespace and replace by -
14+
.replace(/-+/g, '-'); // collapse dashes
15+
16+
return str;
17+
}
18+
19+
let generatePost = (data) => {
20+
let date = new Date()
21+
22+
let day = (date.getDate()).toString().padStart(2, '0')
23+
let month = (date.getMonth()+1).toString().padStart(2, '0')
24+
let year = date.getFullYear()
25+
26+
let text = `---
27+
title: ${data.title}
28+
date: ${year}-${month}-${day}
29+
---
30+
31+
${data.content}`
32+
33+
let slug = `${year}-${month}-${day}-${string_to_slug(data.title)}`
34+
let path = `heyworld/${slug}.md`
35+
36+
return { path, slug, text }
37+
}
38+
39+
exports.handler = async function (event, context) {
40+
const { Octokit } = require("@octokit/rest");
41+
const { createTokenAuth } = require("@octokit/auth-token");
42+
const { request } = require("@octokit/request");
43+
44+
let data = JSON.parse(event.body)
45+
46+
let { path, slug, text } = generatePost(data)
47+
48+
const TOKEN = data.gh_token
49+
const auth = createTokenAuth(TOKEN)
50+
const authentication = await auth()
51+
// console.log(authentication)
52+
53+
const octokit = new Octokit({
54+
auth: TOKEN,
55+
});
56+
57+
const response = await octokit.repos.createOrUpdateFileContents({
58+
owner: 'taniki',
59+
repo: '11d.im',
60+
path: path,
61+
message: `✉️ hey world: ${data.title}`,
62+
content: Buffer.from(text, 'utf8').toString('base64')
63+
})
64+
65+
return {
66+
statusCode: 200,
67+
body: "hey"
68+
};
69+
};

netlify.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[functions]
2+
directory = "functions"

0 commit comments

Comments
 (0)