2 files changed +36
-3
lines changed Original file line number Diff line number Diff line change 2
2
3
3
require 'algolia_html_extractor'
4
4
require 'pathname'
5
+ require 'time'
5
6
6
7
module Jekyll
7
8
module Algolia
@@ -234,14 +235,24 @@ def self.categories(file)
234
235
def self . date ( file )
235
236
# Collections get their date from .date, while pages read it from .data.
236
237
# Jekyll by default will set the date of collection to the current date,
237
- # but we overwrote this.
238
+ # but we monkey-patched that so it returns nil for collection items
238
239
date = if file . respond_to? ( :date )
239
240
file . date
240
241
else
241
242
file . data [ 'date' ]
242
243
end
243
244
244
245
return nil if date . nil?
246
+
247
+ # If date is a string, we try to parse it
248
+ if date . is_a? String
249
+ begin
250
+ date = Time . parse ( date )
251
+ rescue StandardError
252
+ return nil
253
+ end
254
+ end
255
+
245
256
date . to_time . to_i
246
257
end
247
258
Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
+ # rubocop:disable Metrics/BlockLength
3
4
require 'spec_helper'
4
5
5
- # rubocop:disable Metrics/BlockLength
6
6
describe ( Jekyll ::Algolia ::FileBrowser ) do
7
7
let ( :current ) { Jekyll ::Algolia ::FileBrowser }
8
8
let ( :configurator ) { Jekyll ::Algolia ::Configurator }
520
520
let ( :filepath ) { 'sample-item.md' }
521
521
it { should eq nil }
522
522
end
523
- describe 'date in frontmatter' do
523
+ describe 'date in frontmatter as DD-MM-YYYY ' do
524
524
let ( :filepath ) { 'collection-item.md' }
525
525
it { should eq 452_469_600 }
526
526
end
527
527
end
528
+
529
+ # Sometimes a conjunction of plugins on the user side can change the format
530
+ # of the date (it should be passed as a date object) and keep it as
531
+ # a string. In order to test those cases, we will manually pass strings to
532
+ # the call
533
+ context 'direct call' do
534
+ subject { current . date ( file ) }
535
+
536
+ let ( :file ) { double ( 'File' ) }
537
+ before do
538
+ allow ( file ) . to receive ( :data ) . and_return ( mock_data )
539
+ end
540
+
541
+ describe 'date as a valid string' do
542
+ let ( :mock_data ) { { 'date' => '1984-05-04' } }
543
+ it { should eq 452_469_600 }
544
+ end
545
+ describe 'date as a invalid string' do
546
+ let ( :mock_data ) { { 'date' => 'azertyuiop' } }
547
+ it { should eq nil }
548
+ end
549
+ end
528
550
end
529
551
530
552
describe '.excerpt_raw' do
0 commit comments