Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add new option to disable anchors in headlines #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions markdown.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export interface ParseOptions {
/** Select output format. Defaults to "html" */
format? : "html" | "xhtml"

/** Disable anchor tag in headlines. Defaults to `false` */
disableHeadlineAnchors? : boolean

/**
* bytes=true causes parse() to return the result as a Uint8Array instead of a string.
*
Expand Down
7 changes: 4 additions & 3 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ typedef int32_t i32;

// these should be in sync with "OutputFlags" in md.js
typedef enum OutputFlags {
OutputFlagHTML = 1 << 0,
OutputFlagXHTML = 1 << 1,
OutputFlagAllowJSURI = 1 << 2, // allow "javascript:" URIs in links
OutputFlagHTML = 1 << 0,
OutputFlagXHTML = 1 << 1,
OutputFlagAllowJSURI = 1 << 2, // allow "javascript:" URIs in links
OutputFlagDisableHeadlineAnchors = 1 << 3,
} OutputFlags;

typedef int(*JSTextFilterFun)(
Expand Down
2 changes: 1 addition & 1 deletion src/fmt_html.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ static int enter_block_callback(MD_BLOCKTYPE type, void* detail, void* userdata)
case MD_BLOCK_H:
{
render_literal(r, head[((MD_BLOCK_H_DETAIL*)detail)->level - 1]);
r->addanchor = 1;
r->addanchor = !(r->flags & OutputFlagDisableHeadlineAnchors);
break;
}
case MD_BLOCK_CODE: render_open_code_block(r, (const MD_BLOCK_CODE_DETAIL*) detail); break;
Expand Down
11 changes: 8 additions & 3 deletions src/md.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ export const ParseFlags = {

// these should be in sync with "OutputFlags" in common.h
const OutputFlags = {
HTML: 1 << 0, // Output HTML
XHTML: 1 << 1, // Output XHTML (only has effect with HTML flag set)
AllowJSURI: 1 << 2, // Allow "javascript:" URIs
HTML: 1 << 0, // Output HTML
XHTML: 1 << 1, // Output XHTML (only has effect with HTML flag set)
AllowJSURI: 1 << 2, // Allow "javascript:" URIs
DisableHeadlineAnchors: 1 << 3, // Disable anchor tag in headlines.
}


Expand All @@ -59,6 +60,10 @@ export function parse(source, options) {

let outputFlags = options.allowJSURIs ? OutputFlags.AllowJSURI : 0

if(options.disableHeadlineAnchors) {
outputFlags |= OutputFlags.DisableHeadlineAnchors;
};

switch (options.format) {
case "xhtml":
outputFlags |= OutputFlags.HTML | OutputFlags.XHTML
Expand Down
30 changes: 30 additions & 0 deletions test/issue22.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// https://github.com/rsms/markdown-wasm/issues/22
const { checkHTMLResult, exit } = require('./testutil');

checkHTMLResult('DISABLE_HEADLINE_ANCHOR', `# Test1
## Test2
### Test3
#### Test4
`, `<h1>Test1</h1>
<h2>Test2</h2>
<h3>Test3</h3>
<h4>Test4</h4>\n`, {
disableHeadlineAnchors: true,
});

checkHTMLResult(
'ENABLE_HEADLINE_ANCHOR',
`# Test1
## Test2
### Test3
#### Test4`,
`<h1><a id="test1" class="anchor" aria-hidden="true" href="#test1"></a>Test1</h1>
<h2><a id="test2" class="anchor" aria-hidden="true" href="#test2"></a>Test2</h2>
<h3><a id="test3" class="anchor" aria-hidden="true" href="#test3"></a>Test3</h3>
<h4><a id="test4" class="anchor" aria-hidden="true" href="#test4"></a>Test4</h4>\n`,
{
disableHeadlineAnchors: false,
}
);

exit();
4 changes: 2 additions & 2 deletions test/testutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ const wave = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
exports.numFailures = 0


exports.checkHTMLResult = function check(name, inputData, expectedOutputData) {
exports.checkHTMLResult = function check(name, inputData, expectedOutputData, options = {}) {
if (typeof inputData == "string") {
inputData = Buffer.from(inputData, "utf8")
}
if (typeof expectedOutputData == "string") {
expectedOutputData = Buffer.from(expectedOutputData, "utf8")
}
const actual = Buffer.from(md.parse(inputData, { asMemoryView: true }))
const actual = Buffer.from(md.parse(inputData, { asMemoryView: true, ...options }))
if (expectedOutputData.compare(actual) == 0) {
log(`${name} OK`)
return true
Expand Down