feat: enable optional inline parsing in headlines

This commit is contained in:
Petra Baranski
2023-08-26 13:03:35 +02:00
parent 211b627a75
commit ce81283b26
4 changed files with 65 additions and 6 deletions

View File

@@ -57,9 +57,11 @@ public:
*/
HeadlineParser(
std::function<void(std::string&)> parseLineCallback,
std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback
std::function<std::shared_ptr<BlockParser>(const std::string& line)> getBlockParserForLineCallback,
bool isInlineParserAllowed = true
)
: BlockParser(parseLineCallback, getBlockParserForLineCallback)
, isInlineParserAllowed(isInlineParserAllowed)
{}
/**
@@ -103,7 +105,7 @@ protected:
bool
isLineParserAllowed() const override
{
return false;
return this->isInlineParserAllowed;
}
void
@@ -131,6 +133,9 @@ protected:
line = std::regex_replace(line, hlRegex[i], hlReplacement[i]);
}
}
private:
bool isInlineParserAllowed;
}; // class HeadlineParser
// -----------------------------------------------------------------------------

View File

@@ -286,12 +286,24 @@ private:
) &&
maddy::HeadlineParser::IsStartingLine(line)
)
{
if (!this->config || this->config->isHeadlineInlineParsingEnabled)
{
parser = std::make_shared<maddy::HeadlineParser>(
[this](std::string& line){ this->runLineParser(line); },
nullptr,
true
);
}
else
{
parser = std::make_shared<maddy::HeadlineParser>(
nullptr,
nullptr
nullptr,
false
);
}
}
else if (
(
!this->config ||

View File

@@ -70,11 +70,22 @@ struct ParserConfig
*/
bool isHTMLWrappedInParagraph;
/**
* en-/disable headline inline-parsing
*
* default: enabled
*/
bool isHeadlineInlineParsingEnabled;
/**
* enabled parsers bitfield
*/
uint32_t enabledParsers;
ParserConfig()
: isEmphasizedParserEnabled(true)
, isHTMLWrappedInParagraph(true)
, isHeadlineInlineParsingEnabled(true)
, enabledParsers(maddy::types::DEFAULT)
{}
}; // class ParserConfig

View File

@@ -64,3 +64,34 @@ TEST(MADDY_PARSER, ItShouldParseWithSmallConfig)
ASSERT_EQ(testHtml3, output);
}
TEST(MADDY_PARSER, ItShouldParseInlineCodeInHeadlines)
{
const std::string headlineTest = R"(
# Some **test** markdown
)";
const std::string expectedHTML = "<h1>Some <strong>test</strong> markdown</h1>";
std::stringstream markdown(headlineTest);
auto parser = std::make_shared<maddy::Parser>();
const std::string output = parser->Parse(markdown);
ASSERT_EQ(expectedHTML, output);
}
TEST(MADDY_PARSER, ItShouldNotParseInlineCodeInHeadlineIfDisabled)
{
const std::string headlineTest = R"(
# Some **test** markdown
)";
const std::string expectedHTML = "<h1>Some **test** markdown</h1>";
std::stringstream markdown(headlineTest);
auto config = std::make_shared<maddy::ParserConfig>();
config->isHeadlineInlineParsingEnabled = false;
auto parser = std::make_shared<maddy::Parser>(config);
const std::string output = parser->Parse(markdown);
ASSERT_EQ(expectedHTML, output);
}