forked from prose/starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cms.html
187 lines (170 loc) · 7.01 KB
/
cms.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
---
---
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" type="text/css"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css" />
<title>Jekyll+GitHub API+JS=Dynamic CMS :-)</title>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="container" id="container1" >
<h5>Create new post at {{ site.title }}</h5>
{% if site.github.repo == none %}
<div class="alert alert-danger" role="alert"><code>github.repo</code> is not set. Check <a href="https://github.com/vrypan/jekyll-post-via-web"
>github.com/vrypan/jekyll-post-via-web</a> for instructions.</div>
{% endif %}
{% if site.github.branch == none %}
<div class="alert alert-danger" role="alert"><code>github.branch</code> is not set. Check <a href="https://github.com/vrypan/jekyll-post-via-web"
>github.com/vrypan/jekyll-post-via-web</a> for instructions.</div>
{% endif %}
<div id="messages"></div>
<form id="theform" method="get">
<div class="form-group">
<input type="text" class="form-control" placeholder="Post Title" id="p_title" name="title">
</div>
<div class="form-group">
<textarea class="form-control" rows="10" id="p_body" placeholder="Type your post here." name="body"></textarea>
</div>
<div class="form-inline">
<div class="form-group">
<label for="username" class="sr-only">Username</label>
<input type="text" class="form-control" id="username" placeholder="GitHub username">
</div>
<div class="form-group">
<label for="password" class="sr-only">password</label>
<input type="password" class="form-control" id="password" placeholder="GitHub password">
</div>
<input type="button" class="btn btn-default" id="go" value="Post" />
<a class="btn btn-link" data-toggle="collapse" href="#submitform" aria-expanded="false" aria-controls="submitform">Remember username/password</a>
</div>
<div class="collapse" id="submitform">
<br>
<div class="well">
<p>Because of the way this form works (the form data is not sumbitted, as forms usually do), your browser may not ask you to remember the username/password.
<br>If you'd like to save them to your password manager, fill them in and click this button.
<br>This will not create a new post!</p>
<input type="submit" class="btn btn-default" value="Trick browser to save username/password">
</div>
</div>
</form>
<p>Drag this link to your bookmarks, to post from any webpage: <a href="javascript:(function(){
var title = document.getElementsByTagName('title')[0].innerHTML;
title = encodeURIComponent(title) ;
var selection = '';
if (window.getSelection) {
selection = window.getSelection().toString();
} else if (document.selection && document.selection.type != 'Control') {
selection = document.selection.createRange().text;
}
selection = encodeURIComponent(selection);
new_window=window.open('{{ "/cms.html" | prepend: site.baseurl | prepend: site.url }}?title='+title+'&selection='+selection+'&url='+encodeURIComponent(document.location.href),'JekyllPopup','resizable,scrollbars,status=0,toolbar=0,menubar=0,titlebar=0,width=640,height=600,location=0');
})();"
>post to GitHub</a></p>
</div>
<script>
function get_query(){
// Source: http://fellowtuts.com/jquery/getting-query-string-values-in-javascript/
var url = document.location.href;
if (url.indexOf('?') == -1) return false ;
var qs = url.substring(url.indexOf('?') + 1).split('&');
for(var i = 0, result = {}; i < qs.length; i++){
qs[i] = qs[i].split('=');
result[qs[i][0]] = decodeURIComponent(qs[i][1].replace(/\+/g, '%20'));
}
return result;
}
function getSelectionText() {
return text;
}
function post_file(title) {
var d = new Date()
var dd = d.getDate()
if ( dd < 10 ) dd = '0' + dd ;
var mm = d.getMonth()+1 ;
if ( mm < 10 ) mm = '0' + mm ;
var yyyy = d.getFullYear() ;
filename = yyyy+'-'+mm+'-'+dd ;
if (!title) {
filename = filename + '-' + +d.getTime() ;
} else {
var slugified = title.toLowerCase().replace(/\W+/g, '-') ;
if (slugified!='-') {
filename = filename + '-' + slugified ;
} else {
filename = filename + '-' + d.getTime();
}
}
return filename
}
function post_date() {
var d = new Date()
return d.toISOString()
}
var API_NEW_POST = 'https://api.github.com/repos/{{ site.github.repo }}/contents/_posts/' ;
$("#go").click(function() {
var user = $('#username').val() ;
var password = $('#password').val() ;
var title = $('#p_title').val() ;
var p_body = '---\n' ;
p_body = p_body + 'layout: post \n' ;
p_body = p_body + 'title: "' + title +'" \n';
p_body = p_body + 'date: '+ post_date() +' \n' ;
/* Setting the category here is not the most elegant way...
p_body = p_body + 'categories: tests \n';
*/
p_body = p_body + '---\n';
p_body = p_body + '\n' + $('#p_body').val();
var posted_ok = false ;
$.ajax({
dataType: "json",
contentType: 'application/json; charset=UTF-8',
url: API_NEW_POST+post_file(title)+'.markdown',
type: "PUT",
data: JSON.stringify({
content: btoa(unescape(encodeURIComponent(p_body))),
message: 'posted via web',
branch: '{{ site.github.branch }}'
}),
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(user+":"+password));
},
success: function(data) {
posted_ok = true ;
$('#container1').html('<h2>Successfully posted to {{ site.title }}</h2') ;
},
error: function(request, status, error) {
responseText = jQuery.parseJSON( request.responseText );
$('#messages').append($('<div class="alert alert-danger alert-dismissible" role="alert">')
.append('<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>')
.append('<strong>' + request.status + '</strong>: ' + responseText.message )
.append('<br>If you are using two-factor-auth, you will ahve to create ')
.append('<a href="https://github.com/settings/tokens">a personal access token</a> with "repo" permissions and use this instead of your password.')
.append('</div>'));
},
})
;
});
query_params = get_query();
if (query_params) {
src_body = '';
if (query_params['title']) {
src_title = query_params['title'] ;
$('#p_title').val(src_title) ;
}
if (query_params['selection']) {
src_body = '> ' + query_params['selection'] ;
}
if (query_params['body']) {
src_body = query_params['body'] ;
}
if (query_params['url']) {
src_body = src_body + ' —['+src_title+'](' + query_params['url'] + ')\n' ;
}
$('#p_body').val(src_body) ;
}
</script>
</body>