Skip to content

Commit

Permalink
wrap block quotes, convert split links
Browse files Browse the repository at this point in the history
- IMPROVED: Wrap block quotes to max width
- FIXED: Long links that were wrapped were not being replaced when converting to reference links
  • Loading branch information
ttscoff committed Nov 27, 2023
1 parent 76dfbae commit 73939fa
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
11 changes: 7 additions & 4 deletions lib/mdless/colors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ def size_clean
end

def wrap(width=78,foreground=:x)

if self.uncolor =~ /(^([%~] |\s*>)| +[=\-]{5,})/
return self
end
Expand All @@ -145,7 +144,9 @@ def wrap(width=78,foreground=:x)

line += self.match(/^\s*/)[0].gsub(/\t/,' ')
input = self.dup # .gsub(/(\w-)(\w)/,'\1 \2')

input.gsub!(/\[.*?\]\(.*?\)/) do |link|
link.gsub(/ /, "\u00A0")
end
input.split(/\s+/).each do |word|
last_ansi = line.scan(/\e\[[\d;]+m/)[-1] || ''
if visible_width + word.size_clean >= width
Expand All @@ -160,8 +161,10 @@ def wrap(width=78,foreground=:x)
line << " " << last_ansi + word
end
end
lines << line + self.match(/\s*$/)[0] + xc(foreground) if line
return lines.join("\n") # .gsub(/\- (\S)/,'-\1')
lines << line + match(/\s*$/)[0] + xc(foreground) if line
lines.join("\n").gsub(/\[.*?\]\(.*?\)/) do |link|
link.gsub(/\u00A0/, ' ')
end
end

def c(args)
Expand Down
4 changes: 3 additions & 1 deletion lib/mdless/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,12 @@ def block_code(code, language)

def block_quote(quote)
ret = "\n\n"
quote.split("\n").each do |line|
quote.wrap(MDLess.cols, color('blockquote color')).split(/\n/).each do |line|
ret += [
color('blockquote marker color'),
MDLess.theme['blockquote']['marker']['character'],
color('blockquote color'),
' ',
line,
"\n"
].join('')
Expand All @@ -190,6 +191,7 @@ def block_html(raw_html)
def header(text, header_level)
pad = ''
ansi = ''
text.clean_header_ids!
case header_level
when 1
ansi = color('h1 color')
Expand Down
4 changes: 2 additions & 2 deletions lib/mdless/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,8 @@ def highest_header(input)

def clean_markers(input)
input.gsub!(/^(\e\[[\d;]+m)?[%~] ?/, '\1')
input.gsub!(/^(\e\[[\d;]+m)*>(\e\[[\d;]+m)?( +)/, ' \3\1\2')
input.gsub!(/^(\e\[[\d;]+m)*>(\e\[[\d;]+m)?/, '\1\2')
# input.gsub!(/^(\e\[[\d;]+m)*>(\e\[[\d;]+m)?( +)/, ' \3\1\2')
# input.gsub!(/^(\e\[[\d;]+m)*>(\e\[[\d;]+m)?/, '\1\2')
input.gsub!(/(\e\[[\d;]+m)?@@@(\e\[[\d;]+m)?$/, '')
input
end
Expand Down
8 changes: 8 additions & 0 deletions lib/mdless/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ def to_rx(distance: 2, string_start: false)
/#{pre}#{chars.join(".{,#{distance}}")}.*?$/
end

def clean_header_ids!
replace clean_header_ids
end

def clean_header_ids
gsub(/ +\[.*?\] *$/, '').gsub(/ *\{#.*?\} *$/, '').strip
end

def color_meta(cols)
@cols = cols
input = dup
Expand Down

0 comments on commit 73939fa

Please sign in to comment.