Repository: naturalcrit/homebrewery (js)

Issue:
[EDITOR] Table of Contents internal links don't work in <div>
### Renderer

v3

### Browser

Chrome

### Operating System

Windows

### What happened?

Generating a table of contents from the snippet in Legacy results in a section of code that does not function correctly if the brew is later transitioned to V3.

Digging deeper for a minimal reproduction, this bug affects any link inside a `<div>`. Crucially, this bug does NOT appear if there is a line between the opening `<div>` line and the line the link is on.

Investigation has shown that the V3 Markdown `html` block processing code returns the internal text of the block after being parsed by Marked - but crucially, this parsed data is rendered by the default renderer, NOT the custom renderer.

It appears that the solution is to change `/shared/naturalcrit/markdown.js:L14` from:

    return `${openTag} ${Marked.parse(html)} </div>`;

to:

    return `${openTag} ${Marked.parse(html, { renderer: renderer })} </div>`;

---

Discovered while investigating this issue on Reddit: https://redd.it/1158cwd

---

Minimal issue reproduction code is attached.

### Code

```gfm
<div>
[Has no _self Attribute](#p1)
</div>
```


Pull request description:
[EDITOR] Fix div links #2680
This PR fixes #2680.

This PR adds the renderer option to the HTML block function in the V3 Markdown processor, which causes internal links in `<div>` blocks to be processed correctly - specifically, the output has the `target=_self` attribute.

Patch:
diff --git a/changelog.md b/changelog.md
index afe3034d..aed9390c 100644
--- a/changelog.md
+++ b/changelog.md
@@ -80,6 +80,15 @@ pre {
 ## changelog
 For a full record of development, visit our [Github Page](https://github.com/naturalcrit/homebrewery).
 
+### XXXXday DD/MM/2023 - v3.8.0
+{{taskList
+##### G-Ambatte
+
+* [x] Fix internal links inside `<div>` blocks not automatically receiving the `target=_self` attribute
+
+Fixes issues [#2680](https://github.com/naturalcrit/homebrewery/issues/2680)
+}}
+
 ### Monday 13/03/2023 - v3.7.2
 {{taskList
 
diff --git a/shared/naturalcrit/markdown.js b/shared/naturalcrit/markdown.js
index 8dbb1d7f..16623b8a 100644
--- a/shared/naturalcrit/markdown.js
+++ b/shared/naturalcrit/markdown.js
@@ -239,7 +239,7 @@ const definitionLists = {
 Marked.use({ extensions: [mustacheSpans, mustacheDivs, mustacheInjectInline, definitionLists] });
 Marked.use(MarkedExtendedTables());
 Marked.use(mustacheInjectBlock);
-Marked.use({ smartypants: true });
+Marked.use({ renderer: renderer, smartypants: true });
 
 //Fix local links in the Preview iFrame to link inside the frame
 renderer.link = function (href, title, text) {
@@ -347,10 +347,7 @@ module.exports = {
 	render : (rawBrewText)=>{
 		rawBrewText = rawBrewText.replace(/^\\column$/gm, `\n<div class='columnSplit'></div>\n`)
 														 .replace(/^(:+)$/gm, (match)=>`${`<div class='blank'></div>`.repeat(match.length)}\n`);
-		return Marked.parse(
-			sanatizeScriptTags(rawBrewText),
-			{ renderer: renderer }
-		);
+		return Marked.parse(sanatizeScriptTags(rawBrewText));
 	},
 
 	validate : (rawBrewText)=>{


Test patch:
diff --git a/tests/markdown/basic.test.js b/tests/markdown/basic.test.js
index 174e43d7..459bb22c 100644
--- a/tests/markdown/basic.test.js
+++ b/tests/markdown/basic.test.js
@@ -13,3 +13,9 @@ test('Processes the markdown within an HTML block if its just a class wrapper',
 	const rendered = Markdown.render(source);
 	expect(rendered).toBe('<div> <p><em>Bold text</em></p>\n </div>');
 });
+
+test('Check markdown is using the custom renderer; specifically that it adds target=_self attribute to internal links in HTML blocks', function() {
+	const source = '<div>[Has _self Attribute?](#p1)</div>';
+	const rendered = Markdown.render(source);
+	expect(rendered).toBe('<div> <p><a href="#p1" target="_self">Has _self Attribute?</a></p>\n </div>');
+});