|
| 1 | +import { TableCell } from 'mdast'; |
| 2 | +import { naturalCompare } from 'src/helpers/NaturalSorter'; |
1 | 3 | import { MarkdownTableConverter } from 'src/markdown/MarkdownTableConverter'; |
2 | 4 | import { MarkdownTableFactory } from 'src/markdown/MarkdownTableFactory'; |
3 | 5 | import { RegexFactory } from 'src/regex/RegexFactory'; |
@@ -116,24 +118,47 @@ export class HanayamaHuzzlesRecipe implements Recipe { |
116 | 118 | ); |
117 | 119 | } |
118 | 120 |
|
119 | | - #huzzlesToMarkdownTableString(headers: string[], huzzles: HanayamaHuzzle[]): string { |
| 121 | + #huzzlesToMarkdownTableString(headers: string[], syncedHuzzles: HanayamaHuzzle[], withdrawnModifiedHuzzles: HanayamaHuzzle[]): string { |
120 | 122 | const headerRow = this.markdownTableFactory.tableRowNode( |
121 | 123 | headers.map(header => this.markdownTableFactory.textTableCellNode(header)) |
122 | 124 | ); |
123 | | - const huzzleRows = huzzles.map(huzzle => |
124 | | - this.markdownTableFactory.tableRowNode([ |
125 | | - this.markdownTableFactory.textTableCellNode(huzzle.level), |
126 | | - this.markdownTableFactory.textTableCellNode(huzzle.index), |
127 | | - this.markdownTableFactory.textTableCellNode(huzzle.name), |
128 | | - this.markdownTableFactory.tableCellNode( |
129 | | - this.markdownTableFactory.interleave( |
130 | | - huzzle.imageLinks.map(imageLink => this.markdownTableFactory.imageNode(imageLink, 100)), |
131 | | - this.markdownTableFactory.textNode(' ') |
132 | | - ) |
133 | | - ), |
134 | | - this.markdownTableFactory.textTableCellNode(huzzle.status) |
135 | | - ]) |
136 | | - ); |
| 125 | + const huzzleRows = [ |
| 126 | + ...syncedHuzzles, |
| 127 | + ...withdrawnModifiedHuzzles |
| 128 | + ] |
| 129 | + .sort((first, second) => { |
| 130 | + const stringToNumber = (string: string) => |
| 131 | + isNaN(Number(string)) ? Infinity : Number(string); |
| 132 | + const compare = (first: number, second: number) => |
| 133 | + first < second ? -1 : first > second ? 1 : 0; |
| 134 | + |
| 135 | + return ( |
| 136 | + compare(stringToNumber(first.level), stringToNumber(second.level)) |
| 137 | + || compare(stringToNumber(first.index), stringToNumber(second.index)) |
| 138 | + || naturalCompare(first.name, second.name) |
| 139 | + ); |
| 140 | + }) |
| 141 | + .map(huzzle => { |
| 142 | + let nameTableCell: TableCell; |
| 143 | + if (withdrawnModifiedHuzzles.includes(huzzle)) { |
| 144 | + nameTableCell = this.markdownTableFactory.deletedTextTableCellNode(huzzle.name); |
| 145 | + } else { |
| 146 | + nameTableCell = this.markdownTableFactory.textTableCellNode(huzzle.name); |
| 147 | + } |
| 148 | + |
| 149 | + return this.markdownTableFactory.tableRowNode([ |
| 150 | + this.markdownTableFactory.textTableCellNode(huzzle.level), |
| 151 | + this.markdownTableFactory.textTableCellNode(huzzle.index), |
| 152 | + nameTableCell, |
| 153 | + this.markdownTableFactory.tableCellNode( |
| 154 | + this.markdownTableFactory.interleave( |
| 155 | + huzzle.imageLinks.map(imageLink => this.markdownTableFactory.imageNode(imageLink, 100)), |
| 156 | + this.markdownTableFactory.textNode(' ') |
| 157 | + ) |
| 158 | + ), |
| 159 | + this.markdownTableFactory.textTableCellNode(huzzle.status) |
| 160 | + ]); |
| 161 | + }); |
137 | 162 | const table = this.markdownTableFactory.table(headerRow, huzzleRows); |
138 | 163 |
|
139 | 164 | return this.markdownTableConverter.tableToString(table); |
|
0 commit comments