Skip to content

Commit

Permalink
Add slate-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanoverna committed Nov 22, 2021
1 parent e034ba0 commit f4d9a19
Show file tree
Hide file tree
Showing 12 changed files with 847 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/slate-utils/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../.eslintrc.js');
21 changes: 21 additions & 0 deletions packages/slate-utils/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) [year] [fullname]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
17 changes: 17 additions & 0 deletions packages/slate-utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# `datocms-structured-text-slate-utils`

A set of Typescript types and helpers to convert Structured Text dast to Slate structures.

## Installation

Using [npm](http://npmjs.org/):

```sh
npm install datocms-structured-text-slate-utils
```

Using [yarn](https://yarnpkg.com/):

```sh
yarn add datocms-structured-text-slate-utils
```
7 changes: 7 additions & 0 deletions packages/slate-utils/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe('datocms-structured-text-slate-utils', () => {
describe('definitions', () => {
it('are coherent', () => {
expect(true).toBeTruthy();
});
});
});
146 changes: 146 additions & 0 deletions packages/slate-utils/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions packages/slate-utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "datocms-structured-text-slate-utils",
"version": "1.2.0",
"description": "A set of helpers to translate DatoCMS Structured Text fields into Slate structures",
"keywords": [
"datocms",
"structured-text"
],
"author": "Stefano Verna <[email protected]>",
"homepage": "https://github.com/datocms/structured-text/tree/master/packages/datocms-structured-text-slate-utils#readme",
"license": "MIT",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"typings": "dist/types/index.d.ts",
"sideEffects": false,
"directories": {
"lib": "dist",
"test": "__tests__"
},
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "git+https://github.com/datocms/structured-text.git"
},
"scripts": {
"build": "tsc && tsc --project ./tsconfig.esnext.json",
"prebuild": "rimraf dist"
},
"bugs": {
"url": "https://github.com/datocms/structured-text/issues"
},
"dependencies": {
"datocms-structured-text-utils": "1.2.0",
"lodash-es": "^4.17.21",
"slate": "0.60.1"
},
"devDependencies": {
"@types/lodash-es": "^4.17.5"
}
}
77 changes: 77 additions & 0 deletions packages/slate-utils/src/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Node as SlateNode } from 'slate';
import {
Block,
blockDef,
Blockquote,
blockquoteDef,
blockquoteSourceDef,
Code,
codeDef,
Heading,
headingDef,
InlineItem,
inlineItemDef,
InlineNode,
inlineNodes,
ItemLink,
itemLinkDef,
Link,
linkDef,
List,
listDef,
ListItem,
listItemDef,
NonTextNode,
Paragraph,
paragraphDef,
Text,
ThematicBreak,
thematicBreakDef,
} from './types';

export const isNonTextNode = (node: SlateNode): node is NonTextNode =>
'type' in node;

export const isText = (node: SlateNode): node is Text =>
!isNonTextNode(node) && 'text' in node;

export const isThematicBreak = (
element: NonTextNode,
): element is ThematicBreak => element.type === thematicBreakDef.type;

export const isParagraph = (element: NonTextNode): element is Paragraph =>
element.type === paragraphDef.type;

export const isBlockquoteSource = (
element: NonTextNode,
): element is Paragraph => element.type === blockquoteSourceDef.type;

export const isHeading = (element: NonTextNode): element is Heading =>
element.type === headingDef.type;

export const isLink = (element: NonTextNode): element is Link =>
element.type === linkDef.type;

export const isItemLink = (element: NonTextNode): element is ItemLink =>
element.type === itemLinkDef.type;

export const isInlineItem = (element: NonTextNode): element is InlineItem =>
element.type === inlineItemDef.type;

export const isBlock = (element: NonTextNode): element is Block =>
element.type === blockDef.type;

export const isList = (element: NonTextNode): element is List =>
element.type === listDef.type;

export const isListItem = (element: NonTextNode): element is ListItem =>
element.type === listItemDef.type;

export const isBlockquote = (element: NonTextNode): element is Blockquote =>
element.type === blockquoteDef.type;

export const isCode = (element: NonTextNode): element is Code =>
element.type === codeDef.type;

export const isInlineNode = (element: NonTextNode): element is InlineNode =>
inlineNodes.map((def) => def.type).includes(element.type);
1 change: 1 addition & 0 deletions packages/slate-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './slateToDast';
Loading

0 comments on commit f4d9a19

Please sign in to comment.