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 =

Pattern for image: ![alt|dimensions](upload://sha1.ext)

%r{!\[([^\]]*)\]\(upload://([^)]+)\)}
ATTACHMENT_PATTERN =

Pattern for attachment: [filename|attachment](upload://sha1.ext) followed by optional (size)

%r{\[([^\]]*\|attachment)\]\(upload://([^)]+)\)(\s*\([^)]+\))?}

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



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/markbridge/processors/discourse_markdown/detectors/upload.rb', line 36

def detect(input, pos)
  char = input[pos]
  return nil unless char == "!" || char == "["

  remaining = input[pos..]

  if char == "!"
    detect_image(remaining, pos)
  else
    detect_attachment(remaining, pos)
  end
end