diff --git a/server/utils/markdown.js b/server/utils/markdown.js
index a5d22d23..c5355456 100644
--- a/server/utils/markdown.js
+++ b/server/utils/markdown.js
@@ -1,14 +1,27 @@
+import slug from 'slug';
import truncate from 'truncate-html';
import marked, { Renderer } from 'marked';
import highlight from 'highlight.js';
+slug.defaults.mode ='rfc3986';
+
const renderer = new Renderer();
renderer.code = (code, language) => {
const validLang = !!(language && highlight.getLanguage(language));
const highlighted = validLang ? highlight.highlight(language, code).value : code;
return `
${highlighted}
`;
};
-
+renderer.heading = (text, level) => {
+ const headingSlug = slug(text);
+ return `
+
+
+
+
+ ${text}
+
+ `;
+},
marked.setOptions({
renderer: renderer,
diff --git a/src/assets/icons/anchor.svg b/src/assets/icons/anchor.svg
new file mode 100644
index 00000000..f9816437
--- /dev/null
+++ b/src/assets/icons/anchor.svg
@@ -0,0 +1 @@
+
\ No newline at end of file
diff --git a/src/components/Document/Document.js b/src/components/Document/Document.js
index 8d87fadd..6a42f1f3 100644
--- a/src/components/Document/Document.js
+++ b/src/components/Document/Document.js
@@ -20,7 +20,10 @@ class Document extends React.Component {
name={ this.props.document.user.name }
timestamp={ this.props.document.createdAt }
/>
-
+
);
}
diff --git a/src/components/Document/Document.scss b/src/components/Document/Document.scss
index be5400ab..fa6dfcc5 100644
--- a/src/components/Document/Document.scss
+++ b/src/components/Document/Document.scss
@@ -2,3 +2,28 @@
width: 100%;
padding: 20px;
}
+
+.document {
+ h1, h2, h3, h4, h5, h6 {
+ :global {
+ .anchor {
+ visibility: hidden;
+ background-image: url('../../assets/icons/anchor.svg');
+ background-repeat: no-repeat;
+ background-size: 100%;
+ background-position: 0 center;
+ margin-left: -26px;
+ width: 20px;
+ display: inline-block;
+ }
+ }
+
+ &:hover {
+ :global {
+ .anchor {
+ visibility: visible;
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/scenes/DocumentScene/DocumentScene.js b/src/scenes/DocumentScene/DocumentScene.js
index 4c4a1658..1286ca23 100644
--- a/src/scenes/DocumentScene/DocumentScene.js
+++ b/src/scenes/DocumentScene/DocumentScene.js
@@ -17,6 +17,19 @@ class DocumentScene extends React.Component {
this.props.fetchDocumentAsync(documentId);
}
+ componentWillReceiveProps = (nextProps) => {
+ // Scroll to anchor after loading
+ const hash = this.props.location.hash;
+
+ if (nextProps.document && hash) {
+ const name = hash.split('#')[1];
+ setTimeout(() => {
+ const element = document.getElementsByName(name)[0];
+ if (element) element.scrollIntoView()
+ }, 0);
+ }
+ }
+
render() {
const document = this.props.document;
let title;