Skip to content

Commit

Permalink
fix(#85): inline blockquote breaks outline
Browse files Browse the repository at this point in the history
  • Loading branch information
cqroot committed Nov 13, 2023
1 parent 1e796ac commit fd1ca7c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
13 changes: 13 additions & 0 deletions src/markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* eslint-disable no-constant-condition */
export default function removeMarkdownLinks(line: string): string {
let result = line;

// remove Markdown links
while (true) {
const x = result.replace(/\[(.*?)\]\(.*?\)/, '$1');
if (x === result) break;
result = x;
}

return result;
}
30 changes: 29 additions & 1 deletion src/markdownHeaders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,35 @@ test('headers after code highlighting', () => {
});
});

test('spaces before code block', () => {
test('85 markdown links in header', () => {
const headers = markdownHeaders(
readFileSync('./test/85-markdown_links_in_header.md', 'utf-8'),
);
expect(headers.length).toBe(3);
expect(headers[0]).toEqual({
html: 'atextb',
level: 1,
lineno: 0,
number: '1',
slug: 'atextb',
});
expect(headers[1]).toEqual({
html: 'atext1text2b',
level: 1,
lineno: 2,
number: '2',
slug: 'atext1text2b',
});
expect(headers[2]).toEqual({
html: '<code>inline blockquote</code> text',
level: 1,
lineno: 4,
number: '3',
slug: 'inline-blockquote-text',
});
});

test('86 spaces before code block', () => {
const headers = markdownHeaders(
readFileSync('./test/86-spaces_before_code_block.md', 'utf-8'),
);
Expand Down
2 changes: 2 additions & 0 deletions src/markdownHeaders.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import removeMarkdownLinks from './markdown';
import getSlug from './markdownSlug';

const katex = require('katex');
Expand Down Expand Up @@ -78,6 +79,7 @@ export default function markdownHeaders(noteBody: string) {
const headerLevel = match[1].length;
if (headerLevel > 6) continue;
let headerText = match[2] ?? '';
headerText = removeMarkdownLinks(headerText);
const headerHtml = renderInline(headerText);

// header count
Expand Down
12 changes: 5 additions & 7 deletions src/markdownSlug.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import removeMarkdownLinks from './markdown';

const uslug = require('uslug');

/* eslint-disable no-constant-condition, no-useless-escape */
export default function getSlug(line: string): string {
let result = line;

result = removeMarkdownLinks(line);

// remove HTML tags
while (true) {
const x = result.replace(/<[^\/][^>]*>([^<>]*?)<\/[^>]*>/, '$1');
Expand All @@ -17,13 +22,6 @@ export default function getSlug(line: string): string {
result = x;
}

// remove Markdown links
while (true) {
const x = result.replace(/\[(.*?)\]\(.*?\)/, '$1');
if (x === result) break;
result = x;
}

// replace '\_' with a temporary string
while (true) {
const x = result.replace(/\\_/g, '$ts$');
Expand Down
5 changes: 5 additions & 0 deletions test/85-markdown_links_in_header.md