Skip to content

Main content

Text as Document Source Code

Browse the implementation files for Text as Document.
tool.ts typescript
import { createToolDocument, type ToolDocument } from '../tool';

export const textAsDocument = (
	input: unknown
): ToolDocument | null => {
	if (typeof input !== 'string' || input.trim() === '')
		return null;
	return createToolDocument(
		input
			.trim()
			.split(/\r?\n\s*\r?\n/)
			.map((text) => ({
				kind: 'paragraph' as const,
				text: text.trim()
			}))
	);
};

export const formatTextAsDocument = (
	input: unknown
): string => {
	const document = textAsDocument(input);
	return document === null
		? ''
		: JSON.stringify(document, null, 2);
};

export const Tool: TypeTool = {
	name: 'Text as Document',
	slug: 'document/from-text',
	intro:
		'Convert plain-text paragraphs into a structured Tool.io document.',
	tool: formatTextAsDocument,
	i: [
		{
			example: 'First paragraph.\n\nSecond paragraph.',
			label: 'Text',
			placeholder: 'Enter plain text',
			render: 'textarea',
			type: 'text'
		}
	],
	o: [
		{
			example:
				'{\n  "blocks": [\n    ...\n  ],\n  "kind": "document",\n  "version": 1\n}',
			label: 'Document',
			placeholder: 'Document will appear here...',
			render: 'textarea',
			type: 'text'
		}
	]
};

export default Tool;

Description

Browse the implementation files for Text as Document.

References

  1. Tool.io on GitHub
  2. Tool.io TypeScript tools library

Similar or alternative tools