lyrics-structure 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +17 -0
- package/dist/index.js +131 -0
- package/dist/test.d.ts +1 -0
- package/dist/test.js +155 -0
- package/package.json +4 -4
- package/{test.ts → test.js} +1 -1
- package/tsconfig.json +2 -2
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Splits text into parts based on bracketed content while maintaining its structure.
|
|
3
|
+
* Does not consider line length or formatting.
|
|
4
|
+
*
|
|
5
|
+
* @param text - The input text to be split into parts
|
|
6
|
+
* @returns An array of content strings
|
|
7
|
+
*/
|
|
8
|
+
export declare const getParts: (text?: string) => string[];
|
|
9
|
+
/**
|
|
10
|
+
* Splits text into natural sections based on text structure.
|
|
11
|
+
* Works with plain text without requiring markdown or special formatting.
|
|
12
|
+
*
|
|
13
|
+
* @param text - The input text to be split into sections
|
|
14
|
+
* @param maxLinesPerSlide - Maximum number of lines to include in a single slide (default: 6)
|
|
15
|
+
* @returns An array of content sections
|
|
16
|
+
*/
|
|
17
|
+
export declare const getSlideParts: (text?: string, maxLinesPerSlide?: number) => string[];
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Splits text into parts based on bracketed content while maintaining its structure.
|
|
3
|
+
* Does not consider line length or formatting.
|
|
4
|
+
*
|
|
5
|
+
* @param text - The input text to be split into parts
|
|
6
|
+
* @returns An array of content strings
|
|
7
|
+
*/
|
|
8
|
+
export const getParts = (text) => {
|
|
9
|
+
if (!text)
|
|
10
|
+
return [];
|
|
11
|
+
// Process parts in brackets and create a map
|
|
12
|
+
const partsMap = new Map();
|
|
13
|
+
// First pass: extract all content with closing tags
|
|
14
|
+
const cleanedText = text.replace(/\[(.*?)\]([\s\S]*?)\[\/\1\]/g, (match, key, content) => {
|
|
15
|
+
if (!partsMap.has(key)) {
|
|
16
|
+
partsMap.set(key, content.trim());
|
|
17
|
+
}
|
|
18
|
+
return `[${key}]`;
|
|
19
|
+
});
|
|
20
|
+
// Second pass: handle validation and solo tags that should reuse content
|
|
21
|
+
const processedText = cleanedText.replace(/\[([^\]]+)\](?!\s*\[\/)(?!\s*\])/g, // Match tags that don't have a closing tag after them
|
|
22
|
+
(match, key) => {
|
|
23
|
+
if (partsMap.has(key)) {
|
|
24
|
+
return match; // Keep the reference if we already have content for this key
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
console.warn(`Warning: Tag [${key}] has no content and was not previously defined`);
|
|
28
|
+
return ''; // Remove invalid tags
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
const result = [];
|
|
32
|
+
const parts = processedText
|
|
33
|
+
.trim()
|
|
34
|
+
.split(/\[([^\]]+)\]/)
|
|
35
|
+
.filter(Boolean);
|
|
36
|
+
parts.forEach((part) => {
|
|
37
|
+
if (partsMap.has(part)) {
|
|
38
|
+
// Add the stored part content
|
|
39
|
+
result.push(partsMap.get(part));
|
|
40
|
+
}
|
|
41
|
+
else if (part.trim()) {
|
|
42
|
+
// Add non-bracketed content
|
|
43
|
+
result.push(part.trim());
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
return result;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Splits text into natural sections based on text structure.
|
|
50
|
+
* Works with plain text without requiring markdown or special formatting.
|
|
51
|
+
*
|
|
52
|
+
* @param text - The input text to be split into sections
|
|
53
|
+
* @param maxLinesPerSlide - Maximum number of lines to include in a single slide (default: 6)
|
|
54
|
+
* @returns An array of content sections
|
|
55
|
+
*/
|
|
56
|
+
export const getSlideParts = (text, maxLinesPerSlide = 6) => {
|
|
57
|
+
if (!text)
|
|
58
|
+
return [];
|
|
59
|
+
// Extract bracketed content first
|
|
60
|
+
const basicParts = getParts(text);
|
|
61
|
+
const result = [];
|
|
62
|
+
basicParts.forEach(part => {
|
|
63
|
+
// Check if content has line breaks that should be respected
|
|
64
|
+
const lines = part.split('\n').filter(line => line.trim().length > 0);
|
|
65
|
+
// If we have multiple lines and more than maxLinesPerSlide, create slides based on line count
|
|
66
|
+
if (lines.length > 1) {
|
|
67
|
+
if (lines.length > maxLinesPerSlide) {
|
|
68
|
+
// Break into multiple slides based on maxLinesPerSlide
|
|
69
|
+
let currentSlide = [];
|
|
70
|
+
lines.forEach(line => {
|
|
71
|
+
if (currentSlide.length >= maxLinesPerSlide) {
|
|
72
|
+
result.push(currentSlide.join('\n'));
|
|
73
|
+
currentSlide = [];
|
|
74
|
+
}
|
|
75
|
+
currentSlide.push(line);
|
|
76
|
+
});
|
|
77
|
+
// Add the final slide if it exists
|
|
78
|
+
if (currentSlide.length > 0) {
|
|
79
|
+
result.push(currentSlide.join('\n'));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
// Split by natural paragraph breaks (empty lines)
|
|
84
|
+
const paragraphs = part.split(/\n\s*\n/)
|
|
85
|
+
.filter(p => p.trim().length > 0)
|
|
86
|
+
.map(p => p.trim());
|
|
87
|
+
// If we have multiple paragraphs, use those as natural breaks
|
|
88
|
+
if (paragraphs.length > 1) {
|
|
89
|
+
result.push(...paragraphs);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
// Just use the whole part as a single slide
|
|
93
|
+
result.push(part);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
// For single large paragraphs (no line breaks), try to find natural sentence groups
|
|
99
|
+
const sentences = part.split(/(?<=[.!?])\s+/)
|
|
100
|
+
.filter(s => s.trim().length > 0);
|
|
101
|
+
// Group sentences into reasonable chunks
|
|
102
|
+
const sentenceGroups = [];
|
|
103
|
+
let currentGroup = [];
|
|
104
|
+
let currentLength = 0;
|
|
105
|
+
sentences.forEach(sentence => {
|
|
106
|
+
// Natural breakpoint based on content and length
|
|
107
|
+
// Group 3-5 sentences together or until we reach ~300 chars
|
|
108
|
+
if (currentGroup.length >= 4 || currentLength > 250) {
|
|
109
|
+
sentenceGroups.push(currentGroup.join(' '));
|
|
110
|
+
currentGroup = [];
|
|
111
|
+
currentLength = 0;
|
|
112
|
+
}
|
|
113
|
+
currentGroup.push(sentence);
|
|
114
|
+
currentLength += sentence.length;
|
|
115
|
+
});
|
|
116
|
+
// Add the last group if it exists
|
|
117
|
+
if (currentGroup.length > 0) {
|
|
118
|
+
sentenceGroups.push(currentGroup.join(' '));
|
|
119
|
+
}
|
|
120
|
+
// If we created multiple groups, use them
|
|
121
|
+
if (sentenceGroups.length > 1) {
|
|
122
|
+
result.push(...sentenceGroups);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
// Otherwise just use the whole part
|
|
126
|
+
result.push(part);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
return result;
|
|
131
|
+
};
|
package/dist/test.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/test.js
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import * as assert from 'assert';
|
|
2
|
+
import { getParts, getSlideParts } from './index.js';
|
|
3
|
+
// Test data
|
|
4
|
+
const lyricsSample = `
|
|
5
|
+
[verse1]
|
|
6
|
+
I still hear your voice when you sleep next to me
|
|
7
|
+
I still feel your touch in my dreams
|
|
8
|
+
Forgive me my weakness, but I don't know why
|
|
9
|
+
Without you it's hard to survive
|
|
10
|
+
[/verse1]
|
|
11
|
+
|
|
12
|
+
[chorus]
|
|
13
|
+
'Cause every time we touch, I get this feeling
|
|
14
|
+
And every time we kiss, I swear I could fly
|
|
15
|
+
Can't you feel my heart beat fast?
|
|
16
|
+
I want this to last
|
|
17
|
+
Need you by my side
|
|
18
|
+
[/chorus]
|
|
19
|
+
|
|
20
|
+
[verse2]
|
|
21
|
+
Your arms are my castle, your heart is my sky
|
|
22
|
+
They wipe away tears that I cry
|
|
23
|
+
The good and the bad times, we've been through them all
|
|
24
|
+
You make me rise when I fall
|
|
25
|
+
[/verse2]
|
|
26
|
+
|
|
27
|
+
[chorus]
|
|
28
|
+
`;
|
|
29
|
+
const bibleSample = `
|
|
30
|
+
In the beginning God created the heavens and the earth. Now the earth was formless and empty, darkness was over the surface of the deep, and the Spirit of God was hovering over the waters.
|
|
31
|
+
|
|
32
|
+
And God said, "Let there be light," and there was light. God saw that the light was good, and he separated the light from the darkness. God called the light "day," and the darkness he called "night." And there was evening, and there was morning—the first day.
|
|
33
|
+
|
|
34
|
+
[psalm23]
|
|
35
|
+
The LORD is my shepherd, I lack nothing.
|
|
36
|
+
He makes me lie down in green pastures,
|
|
37
|
+
he leads me beside quiet waters,
|
|
38
|
+
he refreshes my soul.
|
|
39
|
+
[/psalm23]
|
|
40
|
+
|
|
41
|
+
five
|
|
42
|
+
short
|
|
43
|
+
lines
|
|
44
|
+
should
|
|
45
|
+
be one slide
|
|
46
|
+
|
|
47
|
+
very long line that should be split into two lines but has been sent on one line intead
|
|
48
|
+
`;
|
|
49
|
+
console.log('Running tests for text processing functions...');
|
|
50
|
+
// Test getParts with lyrics
|
|
51
|
+
console.log('\n----- Testing getParts with lyrics -----');
|
|
52
|
+
const lyricsParts = getParts(lyricsSample);
|
|
53
|
+
console.log(`Found ${lyricsParts.length} parts`);
|
|
54
|
+
lyricsParts.forEach((part, i) => {
|
|
55
|
+
console.log(`\nPart ${i + 1}:`);
|
|
56
|
+
console.log(part);
|
|
57
|
+
});
|
|
58
|
+
assert.strictEqual(lyricsParts.length, 4, 'Should extract 4 parts from lyrics (verse1, chorus, verse2, repeated chorus)');
|
|
59
|
+
// Test that the repeated chorus matches the original chorus
|
|
60
|
+
const chorusContent = lyricsParts[1]; // First chorus (index 1)
|
|
61
|
+
const repeatedChorus = lyricsParts[3]; // Last part (index 3) should be repeated chorus
|
|
62
|
+
console.log('\nVerifying repeated chorus:');
|
|
63
|
+
console.log(`Original chorus: "${chorusContent.substring(0, 20)}..."`);
|
|
64
|
+
console.log(`Repeated chorus: "${repeatedChorus.substring(0, 20)}..."`);
|
|
65
|
+
assert.strictEqual(repeatedChorus, chorusContent, 'Repeated chorus should match the original chorus content');
|
|
66
|
+
// Test getParts with Bible verses
|
|
67
|
+
console.log('\n----- Testing getParts with Bible verses -----');
|
|
68
|
+
const bibleParts = getParts(bibleSample);
|
|
69
|
+
console.log(`Found ${bibleParts.length} parts`);
|
|
70
|
+
bibleParts.forEach((part, i) => {
|
|
71
|
+
console.log(`\nPart ${i + 1}:`);
|
|
72
|
+
console.log(part);
|
|
73
|
+
});
|
|
74
|
+
assert.strictEqual(bibleParts.length, 3, 'Should extract 3 parts from Bible verses (main text, psalm23, and the "five short lines" section)');
|
|
75
|
+
// Test getSlideParts with lyrics
|
|
76
|
+
console.log('\n----- Testing getSlideParts with lyrics -----');
|
|
77
|
+
const lyricsSlides = getSlideParts(lyricsSample);
|
|
78
|
+
console.log(`Found ${lyricsSlides.length} slides`);
|
|
79
|
+
lyricsSlides.forEach((slide, i) => {
|
|
80
|
+
console.log(`\nSlide ${i + 1}:`);
|
|
81
|
+
console.log(slide);
|
|
82
|
+
});
|
|
83
|
+
// This should now include the repeated chorus
|
|
84
|
+
assert.strictEqual(lyricsSlides.length, 4, 'Should have 4 slides including the repeated chorus');
|
|
85
|
+
// Test getSlideParts with Bible verses (default maxLinesPerSlide: 6)
|
|
86
|
+
console.log('\n----- Testing getSlideParts with Bible verses (default maxLinesPerSlide) -----');
|
|
87
|
+
const bibleSlides = getSlideParts(bibleSample);
|
|
88
|
+
console.log(`Found ${bibleSlides.length} slides`);
|
|
89
|
+
bibleSlides.forEach((slide, i) => {
|
|
90
|
+
console.log(`\nSlide ${i + 1} (${slide.length} characters):`);
|
|
91
|
+
console.log('-'.repeat(40));
|
|
92
|
+
console.log(slide);
|
|
93
|
+
console.log('-'.repeat(40));
|
|
94
|
+
});
|
|
95
|
+
// Test getSlideParts with Bible verses and custom maxLinesPerSlide = 2
|
|
96
|
+
console.log('\n----- Testing getSlideParts with Bible verses (maxLinesPerSlide: 2) -----');
|
|
97
|
+
const bibleSlidesTighter = getSlideParts(bibleSample, 2);
|
|
98
|
+
console.log(`Found ${bibleSlidesTighter.length} slides (with maxLinesPerSlide: 2)`);
|
|
99
|
+
bibleSlidesTighter.forEach((slide, i) => {
|
|
100
|
+
console.log(`\nSlide ${i + 1} (${slide.length} characters):`);
|
|
101
|
+
console.log('-'.repeat(40));
|
|
102
|
+
console.log(slide);
|
|
103
|
+
console.log('-'.repeat(40));
|
|
104
|
+
});
|
|
105
|
+
// Test handling of long lines that need to be split
|
|
106
|
+
console.log('\n----- Testing getSlideParts with long lines -----');
|
|
107
|
+
const longLineText = `
|
|
108
|
+
This is a normal line.
|
|
109
|
+
This is another normal line.
|
|
110
|
+
very long line that should be split into two lines but has been sent on one line instead and should be handled properly by the getSlideParts function for better readability
|
|
111
|
+
Last normal line.
|
|
112
|
+
`;
|
|
113
|
+
const longLineSlides = getSlideParts(longLineText);
|
|
114
|
+
console.log(`Found ${longLineSlides.length} slides for long line text`);
|
|
115
|
+
longLineSlides.forEach((slide, i) => {
|
|
116
|
+
console.log(`\nSlide ${i + 1}:`);
|
|
117
|
+
console.log('-'.repeat(40));
|
|
118
|
+
console.log(slide);
|
|
119
|
+
console.log('-'.repeat(40));
|
|
120
|
+
});
|
|
121
|
+
// Test edge cases
|
|
122
|
+
console.log('\n----- Testing edge cases -----');
|
|
123
|
+
// Empty string
|
|
124
|
+
const emptyResult = getSlideParts('');
|
|
125
|
+
console.log(`Empty string produced ${emptyResult.length} slides`);
|
|
126
|
+
assert.strictEqual(emptyResult.length, 0, 'Empty string should produce 0 slides');
|
|
127
|
+
// Single line
|
|
128
|
+
const singleLineResult = getSlideParts('Just one line');
|
|
129
|
+
console.log(`Single line produced ${singleLineResult.length} slides`);
|
|
130
|
+
console.log(singleLineResult[0]);
|
|
131
|
+
assert.strictEqual(singleLineResult.length, 1, 'Single line should produce 1 slide');
|
|
132
|
+
assert.strictEqual(singleLineResult[0], 'Just one line', 'Content should match input');
|
|
133
|
+
// Nested tags
|
|
134
|
+
const nestedTagsText = `
|
|
135
|
+
[outer]
|
|
136
|
+
Some text
|
|
137
|
+
[inner]
|
|
138
|
+
Nested content
|
|
139
|
+
[/inner]
|
|
140
|
+
More outer text
|
|
141
|
+
[/outer]
|
|
142
|
+
`;
|
|
143
|
+
const nestedParts = getParts(nestedTagsText);
|
|
144
|
+
console.log('\nNested tags test:');
|
|
145
|
+
console.log(`Found ${nestedParts.length} parts`);
|
|
146
|
+
nestedParts.forEach((part, i) => {
|
|
147
|
+
console.log(`Part ${i + 1}: ${part}`);
|
|
148
|
+
});
|
|
149
|
+
// Test with maxLinesPerSlide = 1 (extreme case)
|
|
150
|
+
console.log('\n----- Testing with maxLinesPerSlide = 1 -----');
|
|
151
|
+
const extremeSlides = getSlideParts(bibleSample, 1);
|
|
152
|
+
console.log(`Found ${extremeSlides.length} slides with maxLinesPerSlide = 1`);
|
|
153
|
+
assert.ok(extremeSlides.length > bibleSlides.length, 'Setting maxLinesPerSlide = 1 should produce more slides');
|
|
154
|
+
console.log('\nAll tests completed successfully!');
|
|
155
|
+
console.log('\nAll additional tests completed successfully!');
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lyrics-structure",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"main": "index.js",
|
|
3
|
+
"version": "1.0.4",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "
|
|
7
|
+
"test": "node dist/test.js",
|
|
8
8
|
"build": "tsc"
|
|
9
9
|
},
|
|
10
10
|
"keywords": [],
|
|
11
11
|
"author": "",
|
|
12
12
|
"license": "ISC",
|
|
13
|
-
"type": "
|
|
13
|
+
"type": "module",
|
|
14
14
|
"description": "",
|
|
15
15
|
"devDependencies": {
|
|
16
16
|
"@types/node": "^22.13.10",
|
package/{test.ts → test.js}
RENAMED
package/tsconfig.json
CHANGED
|
@@ -26,9 +26,9 @@
|
|
|
26
26
|
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
|
27
27
|
|
|
28
28
|
/* Modules */
|
|
29
|
-
"module": "
|
|
29
|
+
"module": "NodeNext", /* Specify what module code is generated. */
|
|
30
|
+
"moduleResolution": "NodeNext", /* Specify how TypeScript looks up a file from a given module specifier. */
|
|
30
31
|
// "rootDir": "./", /* Specify the root folder within your source files. */
|
|
31
|
-
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
|
|
32
32
|
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
33
33
|
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
34
34
|
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|