Skip to content

Instantly share code, notes, and snippets.

@i386net
Last active July 27, 2021 09:40
Show Gist options
  • Save i386net/88c7c00eb8af92e1fedf47bb87891e14 to your computer and use it in GitHub Desktop.
Save i386net/88c7c00eb8af92e1fedf47bb87891e14 to your computer and use it in GitHub Desktop.
for Scrapbox.io | UA localization
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('Enter date: ', 'YYYYMMDD or 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 + ' рік' + ']' + ',',
// '[' + month + '-й місяць' + ']' + ',',
'[' + monthNames[d.getMonth()] + ']' + ',',
// '[' + monthLocal + ']' + ',', //не працює
'[' + 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();
@vitalibondar
Copy link

Замечательно!
Всё работает

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment