Skip to content

Commit

Permalink
LibMarkdown: Render slugified anchor tag in heading
Browse files Browse the repository at this point in the history
Because slugify function accepts AK::String, which can hold unicode
code_points as well, heading text is normalised to ensure with NFD
form to ensure same binary respresentation of a particular string.
  • Loading branch information
tbhaxor committed Oct 4, 2023
1 parent 0cf5054 commit af0c9af
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Userland/Libraries/LibMarkdown/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ set(SOURCES
)

serenity_lib(LibMarkdown markdown)
target_link_libraries(LibMarkdown PRIVATE LibJS LibRegex LibSyntax)
target_link_libraries(LibMarkdown PRIVATE LibUnicode LibJS LibRegex LibSyntax)
6 changes: 5 additions & 1 deletion Userland/Libraries/LibMarkdown/Heading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <AK/Slugify.h>
#include <AK/StringBuilder.h>
#include <LibMarkdown/Heading.h>
#include <LibMarkdown/Visitor.h>
#include <LibUnicode/Normalize.h>

namespace Markdown {

DeprecatedString Heading::render_to_html(bool) const
{
return DeprecatedString::formatted("<h{}>{}</h{}>\n", m_level, m_text.render_to_html(), m_level);
auto input = Unicode::normalize(m_text.render_for_raw_print().view(), Unicode::NormalizationForm::NFD);
auto slugified = MUST(AK::slugify(input));
return DeprecatedString::formatted("<h{} id='{}'><a href='#{}'>#</a> {}</h{}>\n", m_level, slugified, slugified, m_text.render_to_html(), m_level);
}

Vector<DeprecatedString> Heading::render_lines_for_terminal(size_t) const
Expand Down

0 comments on commit af0c9af

Please sign in to comment.