Skip to content

Commit

Permalink
Merge pull request #22 from tanainc/sh/saferDateSplit
Browse files Browse the repository at this point in the history
Allow for weird spaces in day node titles
  • Loading branch information
houshuang authored Feb 26, 2024
2 parents ab46a7b + e6970b1 commit cb4e5d7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/converters/common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@ test('getValueForAttribute', () => {
test('dateStringToUSDateUID', () => {
expect(dateStringToUSDateUID('June 1st, 2021')).toBe('06-01-2021');
expect(dateStringToUSDateUID('August 14th, 2021')).toBe('08-14-2021');
expect(dateStringToYMD('February 13th, 2023')).toBe('2023-02-13');
});

test('dateStringToYMD', () => {
expect(dateStringToYMD('June 1st, 2021')).toBe('2021-06-01');
expect(dateStringToYMD('August 14th, 2021')).toBe('2021-08-14');
expect(dateStringToYMD('February 13th, 2023')).toBe('2023-02-13');
});

test('hasImages', () => {
expect(hasImages('![](https://tana.inc/photo/1)')).toBeTruthy();
expect(hasImages('![](https://tana.inc/photo/1) ![](https://tana.inc/photo/2)')).toBeTruthy();
expect(hasImages('bar ![](https://tana.inc/photo/1) foo ![](https://tana.inc/photo/2) bam')).toBeTruthy();
expect(hasImages('nope')).toBeFalsy();
});
});
16 changes: 8 additions & 8 deletions src/converters/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ const months = [
'December',
];

const monthPrefixes = months.map((m) => m.slice(0, 3))
const monthPrefixes = months.map((m) => m.slice(0, 3));

// Convert 'June 1st, 2021' and 'Jun 1st, 2021' to 'MM-DD-YYYY' without dealing with timezones, etc.
export function dateStringToUSDateUID(str: string) {
str = str.replace(/(^\w+\s\d{1,2})(\w{2}),(\s\d+)/, '$1$3');
const pieces = str.split(' ');
const pieces = str.split(/\s/);

const monthMatch = months.indexOf(pieces[0]);
const monthPrefixMatch = monthPrefixes.indexOf(pieces[0]);
let month
let month;
if (monthMatch !== -1) {
month = monthMatch + 1
month = monthMatch + 1;
} else if (monthPrefixMatch !== -1) {
month = monthPrefixMatch + 1
month = monthPrefixMatch + 1;
} else {
return str
return str;
}

return `${month.toString().padStart(2, '0')}-${pieces[1].toString().padStart(2, '0')}-${pieces[2]
Expand All @@ -47,7 +47,7 @@ export function dateStringToUSDateUID(str: string) {
// Convert 'June 1st, 2021' to 'YYYY-MM-DD' without dealing with timezones etc
export function dateStringToYMD(str: string) {
str = str.replace(/(^\w+\s\d{1,2})(\w{2}),(\s\d+)/, '$1$3');
const pieces = str.split(' ');
const pieces = str.split(/\s/);
const month = months.indexOf(pieces[0]) + 1;
return `${pieces[2].toString().padStart(4, '0')}-${month.toString().padStart(2, '0')}-${pieces[1]
.toString()
Expand Down Expand Up @@ -106,4 +106,4 @@ export function findPreceedingAlias(nodeName: string, aliasEndIndex: number): st
}
}
return alias;
}
}

0 comments on commit cb4e5d7

Please sign in to comment.