23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/legion/extensions/knowledge/helpers/parser.rb', line 23
def parse_markdown(file_path:)
content = ::File.read(file_path, encoding: 'utf-8')
sections = []
current_heading = ::File.basename(file_path, '.*')
current_lines = []
section_path = []
content.each_line do |line|
if line.start_with?('# ')
flush_section(sections, current_heading, section_path, current_lines, file_path) unless current_lines.empty?
current_heading = line.sub(/^#+\s*/, '').chomp
section_path = [current_heading]
current_lines = []
elsif line.start_with?('## ')
flush_section(sections, current_heading, section_path, current_lines, file_path) unless current_lines.empty?
current_heading = line.sub(/^#+\s*/, '').chomp
section_path = section_path.first(1) + [current_heading]
current_lines = []
else
current_lines << line
end
end
flush_section(sections, current_heading, section_path, current_lines, file_path) unless current_lines.empty?
sections.empty? ? [{ heading: ::File.basename(file_path, '.*'), section_path: [], content: content.strip, source_file: file_path }] : sections
end
|