Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add support for editing (not creating) FrontMatter
This adds support for parsing files containing FrontMatter and editing
it as like a code block.

Signed-off-by: Ferdinand Thiessen <[email protected]>
  • Loading branch information
susnux committed Jul 29, 2022
commit 1ee4b069ca17fa08121287fe9e6f2f3beb37df1a
11 changes: 11 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 @@ -79,6 +79,7 @@
"@tiptap/vue-2": "^2.0.0-beta.84",
"markdown-it": "^13.0.0",
"markdown-it-container": "^3.0.0",
"markdown-it-front-matter": "^0.2.3",
"path-normalize": "^6.0.6",
"prosemirror-collab": "^1.3.0",
"prosemirror-inputrules": "^1.2.0",
Expand Down
17 changes: 11 additions & 6 deletions src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ import { extensionHighlight } from '../helpers/mappings.js'
import { createEditor, serializePlainText, loadSyntaxHighlight } from './../EditorFactory.js'
import { createMarkdownSerializer } from './../extensions/Markdown.js'
import markdownit from './../markdownit/index.js'
import markdownitFrontMatter from 'markdown-it-front-matter'

import { Collaboration, Keymap, UserColor } from './../extensions/index.js'
import DocumentStatus from './Editor/DocumentStatus.vue'
Expand Down Expand Up @@ -458,14 +459,18 @@ export default {
},

onLoaded({ documentSource }) {
this.hasConnectionIssue = false
let frontMatter = ''
const rendered = !this.isRichEditor
? `<pre>${escapeHtml(documentSource)}</pre>`
: markdownit.use(markdownitFrontMatter, (fm) => {
frontMatter = `<pre id="frontmatter"><code>${escapeHtml(fm)}</code></pre>`
}).render(documentSource)

const content = this.isRichEditor
? markdownit.render(documentSource)
: '<pre>' + escapeHtml(documentSource) + '</pre>'
const language = extensionHighlight[this.fileExtension] || this.fileExtension
this.hasConnectionIssue = false
const content = frontMatter + rendered
const language = extensionHighlight[this.fileExtension] || this.fileExtension;

loadSyntaxHighlight(language)
(this.isRichEditor ? Promise.resolve() : loadSyntaxHighlight(language))
.then(() => {
this.$editor = createEditor({
content,
Expand Down
2 changes: 2 additions & 0 deletions src/extensions/RichText.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import Code from '@tiptap/extension-code'
import CodeBlock from '@tiptap/extension-code-block'
import HorizontalRule from '@tiptap/extension-horizontal-rule'
import Dropcursor from '@tiptap/extension-dropcursor'
import FrontMatter from './../nodes/FrontMatter.js'
import HardBreak from './HardBreak.js'
import KeepSyntax from './KeepSyntax.js'
import Table from './../nodes/Table.js'
Expand Down Expand Up @@ -89,6 +90,7 @@ export default Extension.create({
}),
Dropcursor,
KeepSyntax,
FrontMatter,
]
if (this.options.link !== false) {
defaultExtensions.push(Link.configure({
Expand Down
47 changes: 47 additions & 0 deletions src/nodes/FrontMatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import TiptapCodeBlock from '@tiptap/extension-code-block'

const FrontMatter = TiptapCodeBlock.extend({
name: 'frontMatter',
addAttributes() {
return {
...this.parent?.(),
class: {
default: 'frontmatter',
rendered: true,
},
}
},
parseHTML: () => {
return [
{
tag: 'pre[id="frontmatter"]',
preserveWhitespace: 'full',
priority: 9001,
attrs: {
language: 'yaml',
},
},
]
},
toMarkdown: (state, node) => {
if (!state.out.match(/^\s*/)) throw Error('FrontMatter must be the first node of the document!')
state.write('')
state.out = ''
state.write('---\n')
state.text(node.textContent, false)
state.ensureNewLine()
state.write('---')
state.closeBlock(node)
},
// FrontMatter are only valid at the begin of a document
draggable: false,
// Override rules from Codeblock
addCommands() {
return {}
},
addInputRules: () => [],
addPasteRules: () => [],
addProseMirrorPlugins: () => [],
})

export default FrontMatter