-
-
Save vitalibondar/22ff3709629c04b2dc908e7b998b46c9 to your computer and use it in GitHub Desktop.
for Scrapbox.io | UA localization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const diffDate = (date, diff) => { | |
// date: '1993-09-11' | |
// diff: +1, -1 | |
console.log('input', date); | |
console.log('diff', diff); | |
date.setDate(date.getDate() + diff); | |
console.log('output', date); | |
const a = [ | |
date.getFullYear(), | |
('00' + (1 + date.getMonth())).slice(-2), | |
('00' + date.getDate()).slice(-2), | |
]; | |
return a.join('-'); | |
}; | |
const main = function() { | |
const days = [ | |
'неділя', | |
'понеділок', | |
'вівторок', | |
'середа', | |
'четвер', | |
'пʼятниця', | |
'субота', | |
]; //масив з локалізованими днями тижня | |
const monthNames = [ | |
'січень', | |
'лютий', | |
'березень', | |
'квітень', | |
'травень', | |
'червень', | |
'липень', | |
'серпень', | |
'вересень', | |
'жовтень', | |
'листопад', | |
'грудень', | |
]; //масив з локалізованими місяцями | |
const dateHandling = () => { | |
//The date should be in one of the following formats: YYYYMMDD, YYYY MM DD, YYYY-MM-DD, YYYY:MM:DD | |
const askDate = prompt('Введіть дату: ', 'YYYYMMDD чи YYYY MM DD'); | |
if (askDate === null) return new Date(); | |
const dates = (str) => { | |
const year = str.slice(0, 4); | |
const month = str.slice(4, 6) - 1; | |
const day = str.slice(6, 8); | |
return [year, month, day]; | |
}; | |
if (askDate.match(/\d{8}/)) { | |
return new Date(...dates(askDate)); | |
} | |
if (askDate.match(/[0-9]{4}(([-:\s])[0-9]{2}){2}/)) { | |
const match = /[0-9]{4}(([-:\s])[0-9]{2}){2}/.exec(askDate); | |
const dateString = askDate.split(match[2]).join(''); | |
return new Date(...dates(dateString)); | |
} | |
return new Date(); | |
}; | |
const d = dateHandling(); | |
const year = d.getFullYear(); | |
const dt = d.getDate(); | |
const month = d.getMonth() + 1; | |
const date = [year, ('00' + month).slice(-2), ('00' + dt).slice(-2)]; | |
const day = days[d.getDay()]; | |
/****** Editing area ******/ | |
const title = date.join('-'); | |
/************************/ | |
const scrapboxProject = location.href.match(/scrapbox.io\/([^\/.]*)/)[1]; | |
const projectUrl = 'https://scrapbox.io/' + scrapboxProject + '/'; | |
var tags = [ | |
'[← ' + projectUrl + encodeURIComponent(diffDate(d, -1)) + ']', | |
//'[' + year + ' рік' + ']' + ',', | |
'[' + year + ']' + ',', | |
// '[' + month + '-й місяць' + ']' + ',', | |
'[' + monthNames[d.getMonth()] + ']' + ',', | |
'[' + day + '' + ']', | |
'[→ ' + projectUrl + encodeURIComponent(diffDate(d, +2)) + ']', | |
]; | |
/****** Editing area ******/ | |
// Hashtags or text | |
const body = encodeURIComponent( | |
/* '[* Справи:]' + | |
'\n' + | |
'[* Що сталося за день:]' + | |
'\n' + | |
'[* Цікаві думки та ідеї:]' + | |
*/ | |
'\n' + | |
tags.join(' ') + | |
'\n' + | |
'#нотатки' | |
); | |
/************************/ | |
const scrapboxUrl = | |
'https://scrapbox.io/' + scrapboxProject + '/' + encodeURIComponent(title); | |
window.open(scrapboxUrl + '?body=' + body); | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment