# frozen_string_literal: true

require 'rails_helper'

describe PrettyText do
  before do
    SiteSetting.queue_jobs = false
  end

  it "can be disabled" do
    SiteSetting.enable_markdown_footnotes = false

    markdown = <<~MD
      Here is a footnote, [^1]

      [^1]: I am one
    MD

    html = <<~HTML
      <p>Here is a footnote, [^1]</p>\n<p>[^1]: I am one</p>
    HTML

    cooked = PrettyText.cook markdown.strip
    expect(cooked).to eq(html.strip)
  end

  it "supports normal footnotes" do
    markdown = <<~MD
      Here is a footnote, [^1] and another. [^test]

      [^1]: I am one

      [^test]: I am one

       test multiline
    MD

    html = <<~HTML
      <p>Here is a footnote, <sup class="footnote-ref"><a href="#fn1" id="fnref1">[1]</a></sup> and another. <sup class="footnote-ref"><a href="#fn2" id="fnref2">[2]</a></sup></p>
      <p>test multiline</p>
      <hr class="footnotes-sep">

      <ol class="footnotes-list">
      <li id="fn1" class="footnote-item">
      <p>I am one <a href="#fnref1" class="footnote-backref">↩︎</a></p>
      </li>
      <li id="fn2" class="footnote-item">
      <p>I am one <a href="#fnref2" class="footnote-backref">↩︎</a></p>
      </li>
      </ol>
    HTML

    cooked = PrettyText.cook markdown.strip
    expect(cooked).to eq(html.strip)
  end

  it "applies unique ids to elements after cooking a post" do
    raw = <<~MD
      Here is a footnote, [^1] and another. [^test]

      [^1]: I am one

      [^test]: I am one

       test multiline
    MD

    post = create_post(raw: raw)
    post.reload

    html = <<~HTML
      <p>Here is a footnote, <sup class="footnote-ref"><a href="#footnote-#{post.id}-1" id="footnote-ref-#{post.id}-1">[1]</a></sup> and another. <sup class="footnote-ref"><a href="#footnote-#{post.id}-2" id="footnote-ref-#{post.id}-2">[2]</a></sup></p>
      <p>test multiline</p>
      <hr class="footnotes-sep">

      <ol class="footnotes-list">
      <li id="footnote-#{post.id}-1" class="footnote-item">
      <p>I am one <a href="#footnote-ref-#{post.id}-1" class="footnote-backref">↩︎</a></p>
      </li>
      <li id="footnote-#{post.id}-2" class="footnote-item">
      <p>I am one <a href="#footnote-ref-#{post.id}-2" class="footnote-backref">↩︎</a></p>
      </li>
      </ol>
    HTML

    expect(post.cooked.strip).to eq(html.strip)

  end

end
