MCPcopy
hub / github.com/streetwriters/notesnook / extractTitle

Function extractTitle

packages/core/src/utils/html-parser.ts:258–296  ·  view source on GitHub ↗
(html: string, characterLimit: number)

Source from the content-addressed store, hash-verified

256const TITLE_SOURCE_TAGS = ["p", "h1", "h2", "h3", "h4", "h5", "h6"];
257
258export function extractTitle(html: string, characterLimit: number) {
259 let text = "";
260 let rootTag: string | undefined = undefined;
261 const parser = new Parser(
262 {
263 onopentag: (name) => {
264 if (!rootTag && TITLE_SOURCE_TAGS.includes(name)) {
265 rootTag = name;
266 }
267 },
268 onclosetag: (name) => {
269 if (name === rootTag) {
270 if (text) {
271 parser.pause();
272 parser.end();
273 } else {
274 rootTag = undefined;
275 }
276 }
277 },
278 ontext: (data) => {
279 if (!rootTag) return;
280
281 text += data;
282 if (text.length > characterLimit) {
283 text = text.slice(0, characterLimit);
284 parser.pause();
285 parser.end();
286 }
287 }
288 },
289 {
290 lowerCaseTags: false,
291 decodeEntities: true
292 }
293 );
294 parser.end(html);
295 return text;
296}
297
298type OnTagHandler = (
299 name: string,

Callers 1

toTitleMethod · 0.85

Calls 3

pauseMethod · 0.95
endMethod · 0.95
sliceMethod · 0.80

Tested by

no test coverage detected