Skip to content

Commit 81ecdcb

Browse files
feat(endpoint-micropub): use excerpt method from util package
1 parent 2965e6d commit 81ecdcb

File tree

3 files changed

+3
-29
lines changed

3 files changed

+3
-29
lines changed

packages/endpoint-micropub/lib/jf2.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getDate, md5, slugify } from "@indiekit/util";
1+
import { excerpt, getDate, md5, slugify } from "@indiekit/util";
22
import {
33
fetchReferences,
44
mf2tojf2,
@@ -7,12 +7,7 @@ import {
77

88
import { markdownToHtml, htmlToMarkdown } from "./markdown.js";
99
import { reservedProperties } from "./reserved-properties.js";
10-
import {
11-
decodeQueryParameter,
12-
excerptString,
13-
relativeMediaPath,
14-
toArray,
15-
} from "./utils.js";
10+
import { decodeQueryParameter, relativeMediaPath, toArray } from "./utils.js";
1611

1712
/**
1813
* Create JF2 object from form-encoded request
@@ -286,7 +281,7 @@ export const getSlugProperty = (properties, separator) => {
286281
if (suggested) {
287282
string = suggested;
288283
} else if (name) {
289-
string = excerptString(name, 5);
284+
string = excerpt(name, 5);
290285
} else {
291286
string = md5(published).slice(0, 5);
292287
}

packages/endpoint-micropub/lib/utils.js

-14
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,6 @@ export const decodeQueryParameter = (value) => {
2828
: decodeURIComponent(value.replaceAll("+", " "));
2929
};
3030

31-
/**
32-
* Excerpt the first n words from a string
33-
* @param {string} string - String to excerpt
34-
* @param {number} n - Max number of words
35-
* @returns {string|undefined} Excerpt
36-
* @example excerptString('Foo bar baz', 2) => 'Foo bar'
37-
*/
38-
export const excerptString = (string, n) => {
39-
if (typeof string === "string") {
40-
const excerpt = string.split(/\s+/).slice(0, n).join(" ");
41-
return excerpt;
42-
}
43-
};
44-
4531
/**
4632
* Get post template properties
4733
* @param {object} properties - JF2 properties

packages/endpoint-micropub/test/unit/utils.js

-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { before, describe, it, mock } from "node:test";
33

44
import {
55
decodeQueryParameter,
6-
excerptString,
76
getPostTemplateProperties,
87
relativeMediaPath,
98
renderPath,
@@ -29,12 +28,6 @@ describe("endpoint-media/lib/utils", () => {
2928
assert.equal(decodeQueryParameter("foo+bar"), "foo bar");
3029
});
3130

32-
it("Excerpts the first n words from a string", () => {
33-
const result = excerptString("The quick fox jumped over the lazy fox", 5);
34-
35-
assert.equal(result, "The quick fox jumped over");
36-
});
37-
3831
it("Gets post template properties", () => {
3932
const result = getPostTemplateProperties({
4033
name: "foo",

0 commit comments

Comments
 (0)