-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
208 lines (186 loc) · 7.53 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<title>Markdown to Medium</title>
</head>
<body>
<div class="container">
<h1>Markdown to Medium</h1>
<h3>Easily import Markdown posts into Medium</h3>
<h4><a href="https://pqsec.org/2020/05/25/import-markdown-into-medium.html" target="_blank">Introductory post</a></h4>
<label for="postTitle">Post title</label>
<div class="input-group">
<input type="text" class="form-control" id="postTitle" required>
<div class="invalid-feedback">
Choose post title
</div>
</div>
<label for="markdownText">Post Markdown</label>
<div class="input-group">
<textarea class="form-control" id="markdownText" aria-label="Post in Markdown" rows=10></textarea>
<div class="invalid-feedback">
Provide post contents in markdown
</div>
</div>
<label for="apiKey">Medium API key (<a href="https://help.medium.com/hc/en-us/articles/213480228-Get-an-integration-token-for-your-writing-app" target="_blank">how to get</a>)</label>
<div class="input-group">
<input type="text" class="form-control" id="apiKey" maxlength="65">
<div class="invalid-feedback">
Provide your Medium API key: 65 characters containing numbers 0-9 and letters a-f
</div>
</div>
<label for="corsProxy">CORS proxy</label>
<div class="input-group">
<input type="url" class="form-control" id="corsProxy" value="https://cors.root.workers.dev/">
<div class="invalid-feedback">
Need to use a CORS proxy: you can use https://cors.root.workers.dev/ if you don't have one
</div>
</div>
<p></p>
<p>
<div class="alert d-none" role="alert">
</div>
<button type="button" class="btn btn-primary" id="convertBtn" onclick="tryPost(this)">
<span class="spinner-border spinner-border-sm d-none" id="convertSpin" role="status" aria-hidden="true"></span>
Convert
</button>
<button type="button" class="btn btn-outline-secondary" data-toggle="collapse" data-target="#collapseLog">
Show log
</button>
</p>
<div class="collapse" id="collapseLog">
<div class="input-group">
<textarea class="form-control" id="logText" aria-label="Post in Markdown" rows=5 readonly></textarea>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<script>
function tryPost(btn) {
$('.alert').addClass("d-none");
$('.alert').removeClass("alert-danger");
$('.alert').removeClass("alert-success");
if (!validate()) return;
$( btn ).prop("disabled", true);
$( btn ).html($( btn ).html().replace("Convert", "Converting..."));
$( btn ).children("span").removeClass('d-none');
clearLog();
appendLog("Getting Medium user ID...");
auth = $('#apiKey').prop("value")
proxyFetch("https://cors.root.workers.dev/", "https://api.medium.com/v1/me", {
method: "get",
headers: {
"Authorization": "Bearer " + auth,
"Content-type": "application/json",
"Accept": "application/json",
"Accept-Charset": "utf-8",
}
}).then(response => status(response, gotId, "Failed to retrieve user id")).catch(_ => error("Failed to contact the API"));
}
function markValid(item, isValid) {
if (isValid) {
item.addClass('is-valid');
item.removeClass('is-invalid');
} else {
item.addClass('is-invalid');
item.removeClass('is-valid');
}
}
function validate() {
markValid($('#postTitle'), $('#postTitle').prop("value") !== "");
markValid($('#markdownText'), $('#markdownText').prop("value") !== "");
markValid($('#apiKey'), $('#apiKey').prop("value").match("[A-fa-f0-9]{65}"));
markValid($('#corsProxy'), $('#corsProxy').prop("value") !== "" && $('#corsProxy').prop("validity").valid);
return $('.is-invalid').length == 0;
}
function proxyFetch(proxyurl, url, init) {
init['headers']['X-Corsify-Url'] = url;
init['mode'] = 'cors'
return fetch(proxyurl, init)
}
function error(err) {
appendLog(err);
$('.alert').html(err);
$('.alert').addClass("alert-danger");
$('.alert').removeClass("d-none");
reset();
}
function status(response, next, err) {
if (response.status >= 200 && response.status < 300) {
response.json().then(next);
} else {
let status = response.status;
if (status == 401)
status = "unauthorized";
error("Failed to retrieve user id: " + status);
}
}
function gotId(data) {
if ("data" in data) {
if ("id" in data['data'] && "username" in data['data']) {
appendLog("Publishing for user " + data['data']['username'] + "...");
createPost(data['data']['id']);
return;
}
}
error("Invalid response from the API");
}
function createPost(id) {
let postUrl = "https://api.medium.com/v1/users/" + id + "/posts";
let postData = {
"title": $('#postTitle').prop("value"),
"contentFormat": "markdown",
"content": $('#markdownText').prop("value"),
"publishStatus": "draft",
}
proxyFetch("https://cors.root.workers.dev/", postUrl, {
method: "post",
headers: {
"Authorization": "Bearer " + $('#apiKey').prop("value"),
"Content-type": "application/json",
"Accept": "application/json",
"Accept-Charset": "utf-8",
},
body: JSON.stringify(postData),
}).then(response => status(response, gotPostData, "Failed to publish the post")).catch(_ => error("Failed to contact the API"));
}
function doSuccess(title, postUrl) {
appendLog("Published " + title + " at " + postUrl);
$('.alert').html('Published <strong>' + title + '</strong> at <a href="' + postUrl + '" class="alert-link">' + postUrl + '</a>');
$('.alert').addClass("alert-success");
$('.alert').removeClass("d-none");
}
function gotPostData(data) {
if ("data" in data) {
if ("title" in data['data'] && "url" in data['data']) {
doSuccess(data['data']['title'], data['data']['url']);
reset();
return;
}
}
error("Invalid response from the API");
}
function appendLog(str) {
document.getElementById("logText").value += str + "\n";
}
function clearLog() {
document.getElementById("logText").value = "";
}
function reset() {
let btn = $('#convertBtn');
btn.html(btn.html().replace("Converting...", "Convert"));
btn.children("span").addClass('d-none');
btn.prop("disabled", false);
}
</script>
</body>
</html>