-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* move getTextBetween method * add getText method * refactoring * refactoring * refactoring * move renderText to schema, add generateText method * add GenerateText demo * docs: update * remove demo from html page
- Loading branch information
1 parent
42e8755
commit fe6a3e7
Showing
15 changed files
with
241 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"/> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<script type="module"> | ||
import setup from '../../../../setup/vue.ts' | ||
import source from '@source' | ||
setup('GuideContent/GenerateText', source) | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
context('/src/GuideContent/GenerateText/Vue/', () => { | ||
before(() => { | ||
cy.visit('/src/GuideContent/GenerateText/Vue/') | ||
}) | ||
|
||
// TODO: Write tests | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<template> | ||
<pre><code>{{ output }}</code></pre> | ||
</template> | ||
|
||
<script> | ||
import { generateText } from '@tiptap/core' | ||
import Document from '@tiptap/extension-document' | ||
import Paragraph from '@tiptap/extension-paragraph' | ||
import Text from '@tiptap/extension-text' | ||
import HardBreak from '@tiptap/extension-hard-break' | ||
const json = { | ||
type: 'doc', | ||
content: [ | ||
{ | ||
type: 'paragraph', | ||
content: [ | ||
{ | ||
type: 'text', | ||
text: 'This is a paragraph.', | ||
}, | ||
], | ||
}, | ||
{ | ||
type: 'paragraph', | ||
content: [ | ||
{ | ||
type: 'text', | ||
text: 'Here is another paragraph …', | ||
}, | ||
{ | ||
type: 'hardBreak', | ||
}, | ||
{ | ||
type: 'text', | ||
text: '… with an hard break.', | ||
}, | ||
], | ||
}, | ||
], | ||
} | ||
export default { | ||
computed: { | ||
output() { | ||
return generateText(json, [ | ||
Document, | ||
Paragraph, | ||
Text, | ||
HardBreak, | ||
// other extensions … | ||
], { | ||
// define a custom block separator if you want to | ||
blockSeparator: '\n\n', | ||
}) | ||
}, | ||
}, | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Node } from 'prosemirror-model' | ||
import getSchema from './getSchema' | ||
import { Extensions, JSONContent, TextSerializer } from '../types' | ||
import getTextSeralizersFromSchema from './getTextSeralizersFromSchema' | ||
import getText from './getText' | ||
|
||
export default function generateText( | ||
doc: JSONContent, | ||
extensions: Extensions, | ||
options?: { | ||
blockSeparator?: string, | ||
textSerializers?: Record<string, TextSerializer>, | ||
}, | ||
): string { | ||
const { | ||
blockSeparator = '\n\n', | ||
textSerializers = {}, | ||
} = options || {} | ||
const schema = getSchema(extensions) | ||
const contentNode = Node.fromJSON(schema, doc) | ||
|
||
return getText(contentNode, { | ||
blockSeparator, | ||
textSerializers: { | ||
...textSerializers, | ||
...getTextSeralizersFromSchema(schema), | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { TextSerializer } from '../types' | ||
import { Node as ProseMirrorNode } from 'prosemirror-model' | ||
import getTextBetween from './getTextBetween' | ||
|
||
export default function getText( | ||
node: ProseMirrorNode, | ||
options?: { | ||
blockSeparator?: string, | ||
textSerializers?: Record<string, TextSerializer>, | ||
}, | ||
) { | ||
const range = { | ||
from: 0, | ||
to: node.content.size, | ||
} | ||
|
||
return getTextBetween(node, range, options) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Range, TextSerializer } from '../types' | ||
import { Node as ProseMirrorNode } from 'prosemirror-model' | ||
|
||
export default function getTextBetween( | ||
startNode: ProseMirrorNode, | ||
range: Range, | ||
options?: { | ||
blockSeparator?: string, | ||
textSerializers?: Record<string, TextSerializer>, | ||
}, | ||
): string { | ||
const { from, to } = range | ||
const { | ||
blockSeparator = '\n\n', | ||
textSerializers = {}, | ||
} = options || {} | ||
let text = '' | ||
let separated = true | ||
|
||
startNode.nodesBetween(from, to, (node, pos, parent, index) => { | ||
const textSerializer = textSerializers?.[node.type.name] | ||
|
||
if (textSerializer) { | ||
if (node.isBlock && !separated) { | ||
text += blockSeparator | ||
separated = true | ||
} | ||
|
||
text += textSerializer({ | ||
node, | ||
pos, | ||
parent, | ||
index, | ||
}) | ||
} else if (node.isText) { | ||
text += node?.text?.slice(Math.max(from, pos) - pos, to - pos) | ||
separated = false | ||
} else if (node.isBlock && !separated) { | ||
text += blockSeparator | ||
separated = true | ||
} | ||
}) | ||
|
||
return text | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Schema } from 'prosemirror-model' | ||
import { TextSerializer } from '../types' | ||
|
||
export default function getTextSeralizersFromSchema(schema: Schema): Record<string, TextSerializer> { | ||
return Object.fromEntries(Object | ||
.entries(schema.nodes) | ||
.filter(([, node]) => node.spec.toText) | ||
.map(([name, node]) => [name, node.spec.toText])) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.