Public
Edited
Jun 10, 2023
Insert cell
Insert cell
viewof SRC_NAME = Inputs.text({
value: 'Formatted',
})
Insert cell
viewof SRC_FILE = Inputs.file()
Insert cell
{
const file = SRC_FILE;
if (!file) return;

viewof SRC_NAME.value = file.name;
viewof SRC_NAME.dispatchEvent(new Event('input', { bubbles: true }))
const text = await file.text();
viewof SRC_TEXT.value = text;
viewof SRC_TEXT.dispatchEvent(new Event('input', { bubbles: true }))
}
Insert cell
viewof SRC_TEXT = Inputs.textarea({
rows: 24,
})
Insert cell
viewof DST_TEXT = ((value) => Inputs.textarea({ rows: 24, value }))(
SRC_TEXT
.replaceAll(/^([1-9](?:\.[1-9])*.+)$/gm, '\n\n$1\n\n')
.replaceAll(/(?<=\.)$\n^(?=[A-Z])/gm, '\n\n')
.split(/\n\n+/gm)
.map((paragraph) => (
paragraph
.split(/\n+/gm)
.filter((line) => (
line !== ''
))
.map((line) => (
line
.replace(/^\s*/gm, '')
.replace(/\s*$/gm, '')
))
.join(' ')
))
.map((paragraph) => (
wrap(paragraph, 72)
))
.join('\n\n')
)
Insert cell
viewof DST_NAME = ((value) => Inputs.text({ value }))(
SRC_NAME
.replace(/\.txt$/, ' - Formatted.txt')
)
Insert cell
DOM.download(new Blob([DST_TEXT], { type: 'text/plain' }), `${DST_NAME}`, `Download ${DST_NAME}`)
Insert cell
// Thanks https://stackoverflow.com/a/51506718
function wrap(s, w) {
return s.replace(
new RegExp(`(?![^\\n]{1,${w}}$)([^\\n]{1,${w}})\\s`, 'g'), '$1\n'
);
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more