diff --git a/_plugins/external-posts.rb b/_plugins/external-posts.rb index 91ef8d6..41a6c43 100644 --- a/_plugins/external-posts.rb +++ b/_plugins/external-posts.rb @@ -42,7 +42,16 @@ module ExternalPosts end def create_document(site, source_name, url, content) - slug = content[:title].downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '') + # check if title is composed only of whitespace or foreign characters + if content[:title].gsub(/[^\w]/, '').strip.empty? + # use the source name and last url segment as fallback + slug = "#{source_name.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')}-#{url.split('/').last}" + else + # parse title from the post or use the source name and last url segment as fallback + slug = content[:title].downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '') + slug = "#{source_name.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')}-#{url.split('/').last}" if slug.empty? + end + path = site.in_source_dir("_posts/#{slug}.md") doc = Jekyll::Document.new( path, { :site => site, :collection => site.collections['posts'] } @@ -80,7 +89,7 @@ module ExternalPosts html = HTTParty.get(url).body parsed_html = Nokogiri::HTML(html) - title = parsed_html.at('head title')&.text || '' + title = parsed_html.at('head title')&.text.strip || '' description = parsed_html.at('head meta[name="description"]')&.attr('content') || '' body_content = parsed_html.at('body')&.inner_html || ''