slate.ts 790 B

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