Skip to content

Commit bdc0d09

Browse files
committed
fix: Ignore options overwritten if block element. (fixes crosstype#49)
1 parent 86f4ac1 commit bdc0d09

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/main.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ export class NodeHtmlMarkdown {
3838
const blockElements = this.options.blockElements?.concat(defaultBlockElements) ?? defaultBlockElements;
3939

4040
/* Setup Translator Bases */
41-
ignoredElements?.forEach(el => {
42-
this.translators.set(el, { ignore: true, recurse: false });
43-
this.codeBlockTranslators.set(el, { ignore: true, recurse: false });
44-
})
45-
4641
blockElements?.forEach(el => {
4742
this.translators.set(el, { surroundingNewlines: 2 });
4843
this.codeBlockTranslators.set(el, { surroundingNewlines: 2 });
4944
});
5045

46+
ignoredElements?.forEach(el => {
47+
this.translators.set(el, { ignore: true, recurse: false });
48+
this.codeBlockTranslators.set(el, { ignore: true, recurse: false });
49+
})
50+
5151
/* Add and merge bases with default and custom translator configs */
5252
for (const [ elems, cfg ] of Object.entries({ ...defaultTranslators, ...customTranslators }))
5353
this.translators.set(elems, cfg, true);

test/options.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,25 @@ describe(`Options`, () => {
148148
expect(resWithAll).toBe(`**some text**_more text_`);
149149
});
150150

151+
test(`ignore block elements (issue #49)`, () => {
152+
// Test that ignore option works for block elements like NAV
153+
const htmlWithNav = `<p>Before</p><nav>Navigation content</nav><p>After</p>`;
154+
155+
const instanceIgnoreNav = new NodeHtmlMarkdown({
156+
ignore: ['nav']
157+
});
158+
const resNoNav = instanceIgnoreNav.translate(htmlWithNav);
159+
expect(resNoNav).toBe(`Before\n\nAfter`);
160+
161+
// Test with multiple block elements
162+
const htmlMultiBlock = `<div>Content</div><nav>Nav</nav><section>Section</section>`;
163+
const instanceIgnoreNavSection = new NodeHtmlMarkdown({
164+
ignore: ['nav', 'section']
165+
});
166+
const resNoNavSection = instanceIgnoreNavSection.translate(htmlMultiBlock);
167+
expect(resNoNavSection).toBe(`Content`);
168+
});
169+
151170
test(`blockElements`, () => {
152171
const html = `<em>x</em><strong>yyy</strong><em>x</em><span>text</span>`;
153172
const instanceStrongBlock = new NodeHtmlMarkdown({

0 commit comments

Comments
 (0)