Skip to content

Commit 144adbd

Browse files
committed
Add id to markdown headings
1 parent 06be496 commit 144adbd

File tree

5 files changed

+38
-12
lines changed

5 files changed

+38
-12
lines changed

src/frontend/components/App/index.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { AlgorithmApi, GitHubApi } from '/apis';
2020
import { actions } from '/reducers';
2121
import { extension, refineGist } from '/common/util';
2222
import { exts, languages } from '/common/config';
23-
import { SCRATCH_PAPER_MD } from '/files';
23+
import { CONTRIBUTING_MD } from '/files';
2424
import styles from './stylesheet.scss';
2525

2626
@connect(({ current, env }) => ({ current, env }), actions)
@@ -130,7 +130,7 @@ class App extends BaseComponent {
130130
login: undefined,
131131
gistId,
132132
title: 'Untitled',
133-
files: [SCRATCH_PAPER_MD, language.skeleton],
133+
files: [CONTRIBUTING_MD, language.skeleton],
134134
});
135135
return Promise.resolve();
136136
} else if (gistId) {

src/frontend/core/renderers/MarkdownRenderer/index.jsx

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,39 @@ class MarkdownRenderer extends Renderer {
77
renderData() {
88
const { markdown } = this.props.data;
99

10-
const link = ({ href, ...rest }) => {
10+
const heading = ({ level, children, ...rest }) => {
11+
const HeadingComponent = [
12+
props => <h1 {...props} />,
13+
props => <h2 {...props} />,
14+
props => <h3 {...props} />,
15+
props => <h4 {...props} />,
16+
props => <h5 {...props} />,
17+
props => <h6 {...props} />,
18+
][level - 1];
19+
20+
const idfy = text => text.toLowerCase().trim().replace(/[^\w \-]/g, '').replace(/ /g, '-');
21+
22+
const getText = children => {
23+
return children ? children.filter(child => child).map(child => {
24+
if (typeof child === 'string') return child;
25+
if ('props' in child) return getText(child.props.children);
26+
return '';
27+
}).join('') : '';
28+
};
29+
30+
const id = idfy(getText(children));
31+
1132
return (
33+
<HeadingComponent id={id} {...rest}>
34+
{children}
35+
</HeadingComponent>
36+
);
37+
};
38+
39+
const link = ({ href, ...rest }) => {
40+
return /^#/.test(href) ? (
41+
<a href={href} {...rest} />
42+
) : (
1243
<a href={href} rel="noopener" target="_blank" {...rest} />
1344
);
1445
};
@@ -26,7 +57,8 @@ class MarkdownRenderer extends Renderer {
2657
};
2758

2859
return (
29-
<ReactMarkdown className={styles.markdown} source={markdown} renderers={{ link, image }} escapeHtml={false} />
60+
<ReactMarkdown className={styles.markdown} source={markdown} renderers={{ heading, link, image }}
61+
escapeHtml={false} />
3062
);
3163
}
3264
}

src/frontend/files/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ export const CODE_CPP = createUserFile('skeletons/code.cpp');
1717
export const CODE_JAVA = createUserFile('skeletons/code.java');
1818
export const CODE_JS = createUserFile('skeletons/code.js');
1919
export const README_MD = createProjectFile('algorithm-visualizer/README.md');
20-
export const SCRATCH_PAPER_MD = createProjectFile('scratch-paper/README.md');
20+
export const CONTRIBUTING_MD = createProjectFile('scratch-paper/CONTRIBUTING.md');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../backend/public/algorithms/CONTRIBUTING.md

src/frontend/files/scratch-paper/README.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy