mirror of https://github.com/status-im/codimd.git
Allow importing from GitLab snippet
This commit is contained in:
parent
17daf32239
commit
3dd3e6bc35
|
@ -243,6 +243,15 @@ var lastInfo = {
|
||||||
};
|
};
|
||||||
var personalInfo = {};
|
var personalInfo = {};
|
||||||
var onlineUsers = [];
|
var onlineUsers = [];
|
||||||
|
var fileTypes = {
|
||||||
|
"pl": "perl",
|
||||||
|
"cgi": "perl",
|
||||||
|
"js": "javascript",
|
||||||
|
"php": "php",
|
||||||
|
"sh": "bash",
|
||||||
|
"rb": "ruby",
|
||||||
|
"html": "html"
|
||||||
|
}
|
||||||
|
|
||||||
//editor settings
|
//editor settings
|
||||||
var textit = document.getElementById("textit");
|
var textit = document.getElementById("textit");
|
||||||
|
@ -1192,8 +1201,23 @@ ui.toolbar.import.gist.click(function () {
|
||||||
});
|
});
|
||||||
//import from snippet
|
//import from snippet
|
||||||
ui.toolbar.import.snippet.click(function () {
|
ui.toolbar.import.snippet.click(function () {
|
||||||
|
$.get(serverurl + '/gitlab')
|
||||||
|
.success(function (data) {
|
||||||
|
$("#snippetImportModalAccessToken").val(data.accesstoken);
|
||||||
|
$("#snippetImportModalBaseURL").val(data.baseURL);
|
||||||
|
$("#snippetImportModalContent").prop('disabled', false);
|
||||||
|
$("#snippetImportModalConfirm").prop('disabled', false);
|
||||||
|
$("#snippetImportModalLoading").hide();
|
||||||
|
$("#snippetImportModal").modal('toggle');
|
||||||
|
})
|
||||||
|
.error(function (data) {
|
||||||
|
showMessageModal('<i class="fa fa-gitlab"></i> Import from Snippet', 'Unable to fetch gitlab parameters :(', '', '', false);
|
||||||
|
})
|
||||||
|
.complete(function () {
|
||||||
//na
|
//na
|
||||||
});
|
});
|
||||||
|
return false;
|
||||||
|
});
|
||||||
//import from clipboard
|
//import from clipboard
|
||||||
ui.toolbar.import.clipboard.click(function () {
|
ui.toolbar.import.clipboard.click(function () {
|
||||||
//na
|
//na
|
||||||
|
@ -1370,25 +1394,24 @@ $("#snippetImportModalConfirm").click(function () {
|
||||||
if (!snippeturl) return;
|
if (!snippeturl) return;
|
||||||
$('#snippetImportModal').modal('hide');
|
$('#snippetImportModal').modal('hide');
|
||||||
$("#snippetImportModalContent").val('');
|
$("#snippetImportModalContent").val('');
|
||||||
if (!isValidURL(snippeturl)) {
|
if (!/^.+\/snippets\/.+$/.test(snippeturl)) {
|
||||||
showMessageModal('<i class="fa fa-gitlab"></i> Import from Snippet', 'Not a valid URL :(', '', '', false);
|
showMessageModal('<i class="fa fa-github"></i> Import from Snippet', 'Not a valid Snippet URL :(', '', '', false);
|
||||||
return;
|
|
||||||
} else {
|
} else {
|
||||||
// TODO: Validate against config.gitlab.baseURL
|
|
||||||
ui.spinner.show();
|
ui.spinner.show();
|
||||||
$.get(snippeturl)
|
var accessToken = '?access_token=' + $("#snippetImportModalAccessToken").val();
|
||||||
|
var fullURL = $("#snippetImportModalBaseURL").val() + '/api/v3' + snippeturl;
|
||||||
|
$.get(fullURL + accessToken)
|
||||||
.success(function(data) {
|
.success(function(data) {
|
||||||
if (data.files) {
|
var content = '# ' + (data.title || "Snippet Import");
|
||||||
var contents = "";
|
var fileInfo = data.file_name.split('.');
|
||||||
Object.keys(data.files).forEach(function (key) {
|
$.get(fullURL + '/raw' + accessToken)
|
||||||
contents += key;
|
.success(function (raw) {
|
||||||
contents += '\n---\n';
|
if (raw) {
|
||||||
contents += data.files[key].content;
|
content += "\n\n```";
|
||||||
contents += '\n\n';
|
content += fileTypes[fileInfo[1]] + "=\n";
|
||||||
});
|
content += raw;
|
||||||
replaceAll(contents);
|
content += "\n```";
|
||||||
} else {
|
replaceAll(content);
|
||||||
showMessageModal('<i class="fa fa-gitlab"></i> Import from Snippet', 'Unable to fetch snippet files :(', '', '', false);
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.error(function (data) {
|
.error(function (data) {
|
||||||
|
@ -1397,6 +1420,10 @@ $("#snippetImportModalConfirm").click(function () {
|
||||||
.complete(function () {
|
.complete(function () {
|
||||||
ui.spinner.hide();
|
ui.spinner.hide();
|
||||||
});
|
});
|
||||||
|
})
|
||||||
|
.error(function (data) {
|
||||||
|
showMessageModal('<i class="fa fa-gitlab"></i> Import from Snippet', 'Not a valid Snippet URL :(', '', JSON.stringify(data), false);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue