mirror of
https://github.com/xuthus83/pigallery2.git
synced 2025-01-14 14:43:17 +08:00
Add blogs to right date if grouped by date #711
This commit is contained in:
parent
c6ba505129
commit
232e91c6fc
@ -1,14 +1,15 @@
|
|||||||
import { Pipe, PipeTransform } from '@angular/core';
|
import { Pipe, PipeTransform } from '@angular/core';
|
||||||
import { FileDTO } from '../../../common/entities/FileDTO';
|
import { FileDTO } from '../../../common/entities/FileDTO';
|
||||||
|
import { MDFileDTO } from '../../../common/entities/MDFileDTO';
|
||||||
|
|
||||||
@Pipe({ name: 'mdFiles' })
|
@Pipe({ name: 'mdFiles' })
|
||||||
export class MDFilesFilterPipe implements PipeTransform {
|
export class MDFilesFilterPipe implements PipeTransform {
|
||||||
transform(metaFiles: FileDTO[]): FileDTO[] | null {
|
transform(metaFiles: FileDTO[]): MDFileDTO[] | null {
|
||||||
if (!metaFiles) {
|
if (!metaFiles) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return metaFiles.filter((f: FileDTO): boolean =>
|
return metaFiles.filter((f: FileDTO): boolean =>
|
||||||
f.name.toLocaleLowerCase().endsWith('.md')
|
f.name.toLocaleLowerCase().endsWith('.md')
|
||||||
);
|
) as MDFileDTO[];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import {Utils} from '../../../../../common/Utils';
|
|||||||
import {ContentService} from '../content.service';
|
import {ContentService} from '../content.service';
|
||||||
import {mergeMap, Observable, shareReplay} from 'rxjs';
|
import {mergeMap, Observable, shareReplay} from 'rxjs';
|
||||||
import {MDFilesFilterPipe} from '../../../pipes/MDFilesFilterPipe';
|
import {MDFilesFilterPipe} from '../../../pipes/MDFilesFilterPipe';
|
||||||
|
import {MDFileDTO} from '../../../../../common/entities/MDFileDTO';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class BlogService {
|
export class BlogService {
|
||||||
@ -23,14 +24,17 @@ export class BlogService {
|
|||||||
const dates = content.mediaGroups.map(g => g.date)
|
const dates = content.mediaGroups.map(g => g.date)
|
||||||
.filter(d => !!d).map(d => d.getTime());
|
.filter(d => !!d).map(d => d.getTime());
|
||||||
|
|
||||||
|
const firstMedia = content.mediaGroups[0].media.reduce((p, m) =>
|
||||||
|
Math.min(m.metadata.creationDate, p), Number.MAX_SAFE_INTEGER);
|
||||||
|
|
||||||
const files = this.mdFilesFilterPipe.transform(content.metaFile)
|
const files = this.mdFilesFilterPipe.transform(content.metaFile)
|
||||||
.map(f => this.splitMarkDown(f, dates));
|
.map(f => this.splitMarkDown(f, dates, firstMedia));
|
||||||
|
|
||||||
return (await Promise.all(files)).flat();
|
return (await Promise.all(files)).flat();
|
||||||
}), shareReplay(1));
|
}), shareReplay(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
private async splitMarkDown(file: FileDTO, dates: number[]): Promise<GroupedMarkdown[]> {
|
private async splitMarkDown(file: MDFileDTO, dates: number[], firstMedia: number): Promise<GroupedMarkdown[]> {
|
||||||
const markdown = (await this.getMarkDown(file)).trim();
|
const markdown = (await this.getMarkDown(file)).trim();
|
||||||
|
|
||||||
if (!markdown) {
|
if (!markdown) {
|
||||||
@ -61,13 +65,33 @@ export class BlogService {
|
|||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getDateGroup = (dateNum: number) => {
|
||||||
|
let groupDate = dates.find((d, i) => i > dates.length - 1 ? false : dates[i + 1] > dateNum); //dates are sorted
|
||||||
|
|
||||||
|
// cant find the date. put to the last group (as it was later)
|
||||||
|
if (groupDate === undefined) {
|
||||||
|
groupDate = dates[dates.length - 1];
|
||||||
|
}
|
||||||
|
return groupDate;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
const baseText = markdown.substring(0, matches[0].index).trim();
|
const baseText = markdown.substring(0, matches[0].index).trim();
|
||||||
|
|
||||||
// don't show empty
|
// don't show empty
|
||||||
if (baseText) {
|
if (baseText) {
|
||||||
ret.push({
|
if (file.date === firstMedia) {
|
||||||
text: baseText,
|
ret.push({
|
||||||
file: file
|
text: baseText,
|
||||||
});
|
file: file
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
ret.push({
|
||||||
|
text: baseText,
|
||||||
|
file: file,
|
||||||
|
date: getDateGroup(file.date)
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = 0; i < matches.length; ++i) {
|
for (let i = 0; i < matches.length; ++i) {
|
||||||
@ -75,12 +99,8 @@ export class BlogService {
|
|||||||
// get UTC midnight date
|
// get UTC midnight date
|
||||||
const dateNum = Utils.makeUTCMidnight(new Date(matchedStr.match(dateRgx)[0])).getTime();
|
const dateNum = Utils.makeUTCMidnight(new Date(matchedStr.match(dateRgx)[0])).getTime();
|
||||||
|
|
||||||
let groupDate = dates.find((d, i) => i > dates.length - 1 ? false : dates[i + 1] > dateNum); //dates are sorted
|
const groupDate = getDateGroup(dateNum);
|
||||||
|
|
||||||
// cant find the date. put to the last group (as it was later)
|
|
||||||
if (groupDate === undefined) {
|
|
||||||
groupDate = dates[dates.length - 1];
|
|
||||||
}
|
|
||||||
const text = (i + 1 >= matches.length ? markdown.substring(matches[i].index) : markdown.substring(matches[i].index, matches[i + 1].index)).trim();
|
const text = (i + 1 >= matches.length ? markdown.substring(matches[i].index) : markdown.substring(matches[i].index, matches[i + 1].index)).trim();
|
||||||
|
|
||||||
// don't show empty
|
// don't show empty
|
||||||
|
Loading…
x
Reference in New Issue
Block a user