Value.ts 755 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { Block, Document, Text, Value } from 'slate';
  2. const SCHEMA = {
  3. blocks: {
  4. paragraph: 'paragraph',
  5. codeblock: 'code_block',
  6. codeline: 'code_line',
  7. },
  8. inlines: {},
  9. marks: {},
  10. };
  11. export const makeFragment = (text: string, syntax?: string) => {
  12. const lines = text.split('\n').map(line =>
  13. Block.create({
  14. type: 'code_line',
  15. nodes: [Text.create(line)],
  16. })
  17. );
  18. const block = Block.create({
  19. data: {
  20. syntax,
  21. },
  22. type: 'code_block',
  23. nodes: lines,
  24. });
  25. return Document.create({
  26. nodes: [block],
  27. });
  28. };
  29. export const makeValue = (text: string, syntax?: string) => {
  30. const fragment = makeFragment(text, syntax);
  31. return Value.create({
  32. document: fragment,
  33. SCHEMA,
  34. });
  35. };