Skip to content
Closed
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
4 changes: 2 additions & 2 deletions js/editor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/editor.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-files.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-files.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-public.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-public.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-text.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-text.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-viewer.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-viewer.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/vendors.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/vendors.js.map

Large diffs are not rendered by default.

60 changes: 60 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"@tiptap/suggestion": "^2.0.0-beta.97",
"@tiptap/vue-2": "^2.0.0-beta.84",
"markdown-it": "^13.0.1",
"markdown-it-anchor": "^8.6.4",
"markdown-it-container": "^3.0.0",
"markdown-it-front-matter": "^0.2.3",
"path-normalize": "^6.0.6",
Expand Down
7 changes: 7 additions & 0 deletions src/markdownit/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import MarkdownIt from 'markdown-it'
import anchor from 'markdown-it-anchor'
import taskLists from '@hedgedoc/markdown-it-task-lists'
import underline from './underline.js'
import splitMixedLists from './splitMixedLists.js'
import callouts from './callouts.js'
import keepSyntax from './keepSyntax.js'

import { slugify } from '../nodes/Heading/slug.js'

const markdownit = MarkdownIt('commonmark', { html: false, breaks: false })
.enable('strikethrough')
.enable('table')
Expand All @@ -13,5 +16,9 @@ const markdownit = MarkdownIt('commonmark', { html: false, breaks: false })
.use(underline)
.use(callouts)
.use(keepSyntax)
.use(anchor, {
level: 1,
slugify,
})

export default markdownit
8 changes: 8 additions & 0 deletions src/nodes/Heading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import debounce from 'debounce'
import { setHeadings, extractHeadings } from './extractor.js'

const Heading = TipTapHeading.extend({
addOptions() {
return {
...this.parent?.(),
linkSymbol: '#',
}
},

addAttributes() {
return {
...this.parent(),
Expand All @@ -16,6 +23,7 @@ const Heading = TipTapHeading.extend({
},
}
},

addKeyboardShortcuts() {
return this.options.levels.reduce((items, level) => ({
...items,
Expand Down
112 changes: 112 additions & 0 deletions src/nodes/HeadingView.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<!--
- @copyright Copyright (c) 2022
-
- @license AGPL-3.0-or-later
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-->

<template>
<NodeViewWrapper ref="content"
:as="domElement"
v-bind="node.attrs"
tabindex="-1">
<a aria-hidden="true"
class="anchor-link"
:href="href"
:title="t('text', 'Permalink')"
@click.stop="click">{{ linkSymbol }}</a>
<NodeViewContent />
</NodeViewWrapper>
</template>

<script>
import Vue from 'vue'
import { NodeViewWrapper, NodeViewContent } from '@tiptap/vue-2'
import { useEditorMixin } from '../components/EditorWrapper.provider.js'

export default Vue.extend({
name: 'HeadingView',
components: {
NodeViewWrapper,
NodeViewContent,
},
mixins: [useEditorMixin],
props: {
node: {
type: Object,
required: true,
},
extension: {
type: Object,
required: true,
},
},

computed: {
href() {
return `#${this.node.attrs.id}`
},
domElement() {
const hasLevel = this.extension.options.levels.includes(this.node.attrs.level)
const level = hasLevel ? this.node.attrs.level : this.extension.options.levels[0]
return `h${level}`
},
linkSymbol() {
return this.extension.options.linkSymbol
},
t: () => window.t,
},

methods: {
click() {
this.$refs.content.$el.scrollIntoView()
window.location.hash = this.href
},
},
})
</script>

<style scoped lang="scss">
div.ProseMirror {
/* Anchor links */
h1, h2, h3, h4, h5, h6 {
.anchor-link {
opacity: 0;
padding: 0;
left: -18px;
font-size: max(1em, 16px);
position: absolute;
text-decoration: none;
transition-duration: .15s;
transition-property: opacity;
transition-timing-function: cubic-bezier(.4,0,.2,1);
}

&:hover .anchor-link {
opacity: 0.25;
}
}

// Shrink clickable area of anchor permalinks while editing
&.ProseMirror-focused[contenteditable="true"] {
h1,h2,h3,h4,h5,h6 {
.anchor-link {
width: fit-content;
}
}
}
}
</style>
9 changes: 7 additions & 2 deletions src/tests/markdown.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ describe('Commonmark', () => {
const expected = entry.markdown.includes('__')
? entry.html.replace(/<strong>/g, '<u>').replace(/<\/strong>/g, '</u>')
: entry.html
// Ignore special markup for untouched markdown
expect(markdownit.render(entry.markdown).replace(/<span class="keep-md">([^<]+)<\/span>/g, '$1')).toBe(expected)
try {
// Ignore special markup for untouched markdown
expect(markdownit.render(entry.markdown).replace(/<span class="keep-md">([^<]+)<\/span>/g, '$1')).toBe(expected)
} catch {
// Header anchors
expect(markdownit.render(entry.markdown).replace(/ id=\"[^\"]*\" tabindex=\"-1\"/g, '')).toBe(expected)
}
})
})
})
Expand Down