Class: Markbridge::Processors::DiscourseMarkdown::Detectors::Upload

Inherits:
Base
  • Object
show all
Defined in:
lib/markbridge/processors/discourse_markdown/detectors/upload.rb

Overview

Detects Discourse upload references using upload:// URLs.

Supports two formats:

  • Images: ![alt|dimensions](upload://sha1.ext)

  • Attachments: [filename|attachment](upload://sha1.ext) (size)

Examples:

Image

detector = Upload.new
input = "![logo|64x64](upload://abc123.png)"
match = detector.detect(input, 0)
match.node.type # => :image

Attachment

detector = Upload.new
input = "[doc.pdf|attachment](upload://xyz789.pdf) (1.2 MB)"
match = detector.detect(input, 0)
match.node.type # => :attachment

Constant Summary collapse

IMAGE_PATTERN =

Image: ![alt|dimensions](upload://sha1.ext)

%r{\A!\[(?<alt>[^|\]]*)(?:\|(?<dimensions>[^\]]*))?\]\(upload://(?<url>[^)]+)\)}
ATTACHMENT_PATTERN =

Attachment: [filename|attachment](upload://sha1.ext) (size)

%r{
  \A
  \[(?<filename>[^|\]]*)\|attachment\]
  \(upload://(?<url>[^)]+)\)
  (?:\s*\((?<size>[^)]+)\))?
}xi

Instance Method Summary collapse

Instance Method Details

#detect(input, pos) ⇒ Match?

Attempt to detect an upload at the given position.

Parameters:

  • input (String)

    the full input string

  • pos (Integer)

    current position to check

Returns:

  • (Match, nil)

    match result or nil if no match



43
44
45
46
47
48
49
50
51
# File 'lib/markbridge/processors/discourse_markdown/detectors/upload.rb', line 43

def detect(input, pos)
  remaining = input[pos..]
  case input[pos]
  when "!"
    detect_image(remaining, pos)
  when "["
    detect_attachment(remaining, pos)
  end
end