Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add getText() and generateText() methods (fix #1428) #1875

Merged
merged 9 commits into from
Sep 9, 2021

Conversation

philippkuehn
Copy link
Contributor

This PR adds two methods getText() and generateText(). You can define text rendering of nodes with the renderText config. This is mostly useful for atom nodes like hard breaks, mentions etc.

const HardBreak = Node.create({
  name: 'hardBreak',

  renderText() {
    return '\n'
  },

  // ...
})

usage of getText()

// basic usage
const text = editor.getText()

// with some options
const text = editor.getText({
  // define a custom block separator if you want to
  blockSeparator: '\n',
  // overwrite `renderText` method of nodes
  textSerializers: {
    hardBreak: () => '\n',
  },
})

usage of generateText()

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 = {} // ProseMirror JSON

// basic usage
const text = generateText(json, [
  Document,
  Paragraph,
  Text,
  HardBreak,
  // other extensions …
])

// with some options
const text = generateText(json, [
  Document,
  Paragraph,
  Text,
  HardBreak,
  // other extensions …
], {
  // define a custom block separator if you want to
  blockSeparator: '\n',
  // overwrite `renderText` method of nodes
  textSerializers: {
    hardBreak: () => '\n',
  },
})

@lostdesign
Copy link

Oh damn 🙌 Very nice addition. Comes in handy as I won't need the tokeniser for flexsearch anymore!

@BrianHung
Copy link
Contributor

@philippkuehn I think the clipboardTextSerializer should only be serializing the given slice as opposed to the entire doc.

@philippkuehn
Copy link
Contributor Author

@BrianHung But I serialize only a part of the doc based on the current selection. Is there any issue with the current solution?

@BrianHung
Copy link
Contributor

BrianHung commented Sep 18, 2021

But I serialize only a part of the doc based on the current selection. Is there any issue with the current solution?

Ah nevermind; I see the range = {from, to} as opposed to the slice. 😅

@tsl1127
Copy link

tsl1127 commented Oct 27, 2023

Is there any way to make the positions of the text output by getText() the same as the positons in the editor? Especially the text in the table

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants