diff --git a/app/helpers/site_helper.rb b/app/helpers/site_helper.rb index 36039ed..61e6208 100644 --- a/app/helpers/site_helper.rb +++ b/app/helpers/site_helper.rb @@ -25,4 +25,29 @@ module SiteHelper rand(range) end + + def parse_article_html(html) + result = {} + return result if html.blank? + + doc = Nokogiri::HTML.fragment(html, 'utf-8') + + first_link = doc.at_css('a') + first_link.remove if first_link + + result[:title] = doc.at_css('h1').text + result[:description] = doc.at_css('div').text + result[:link] = first_link&.to_html + + doc.search('*').each do |node| + while node.children.last and node.children.last.name == 'br' + node.children.last.remove + end + end + + result[:html] = doc.to_html + + result + end + end diff --git a/app/javascript/application.js b/app/javascript/application.js index 5849231..7036b4e 100644 --- a/app/javascript/application.js +++ b/app/javascript/application.js @@ -44,10 +44,24 @@ document.querySelectorAll('#confirm_btn').forEach((confirm_btn) => { }) // Copy link to clipboard -document.querySelectorAll('[data-share-url]').forEach((share_btn) => { +document.querySelectorAll('[data-share-title]').forEach((share_btn) => { share_btn.addEventListener('click', (e) => { - navigator.clipboard.writeText(e.currentTarget.getAttribute('data-share-url')) - alert('URL copied to clipboard!') + + if (navigator.share) { + navigator.share({ + title: e.currentTarget.getAttribute('data-share-title'), + text: e.currentTarget.getAttribute('data-share-text'), + url: window.location.href + }).then(() => { + console.log('Thanks for sharing!'); + }) + .catch(console.error); + } else { + navigator.clipboard.writeText(window.location.href) + alert('URL copied to clipboard!') + } + + }) }) diff --git a/app/views/site/tmpl_article.html.erb b/app/views/site/tmpl_article.html.erb index 3dba3d6..e4bc851 100644 --- a/app/views/site/tmpl_article.html.erb +++ b/app/views/site/tmpl_article.html.erb @@ -1,4 +1,12 @@ -<%- content_for :title, node_title(@node) %> +<%- + + attachment = @node.attachments.limit(1)[0] + + article_parts = parse_article_html(attachment&.body) + + content_for :title, @node.page_title.blank? ? article_parts[:title] : @node.page_title + content_for :meta_description, @node.page_description.blank? ? article_parts[:description] : @node.page_description +%>