Skip to content

Commit

Permalink
feat: support showing cpl.name on result
Browse files Browse the repository at this point in the history
  • Loading branch information
linonetwo committed Nov 25, 2024
1 parent 56c1d18 commit 676da1d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/autocomplete/plugin.info
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"author": "linonetwo",
"description": "Auto complete full-text search across wiki. Dependency of TW-CommandPalette. Install that if you want out-of-box features.",
"plugin-type": "plugin",
"version": "0.3.0",
"version": "0.4.0",
"stability": "STABILITY_1_EXPERIMENTAL",
"list": "readme example config tree"
}
26 changes: 15 additions & 11 deletions src/autocomplete/widgets/build-in-sub-plugins/search-user-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ import { checkIsSearchUser, checkIsUnderFilter } from '../utils/checkPrefix';
import { titleTextExclusionFilter } from '../utils/configs';
import { debounced } from '../utils/debounce';
import { filterTiddlersAsync } from '../utils/filterTiddlersAsync';
import { getFieldsAsText } from '../utils/getFieldsAsTitle';
import { getFieldsAsText, getFieldsAsTitle } from '../utils/getFieldsAsTitle';
import { lingo } from '../utils/lingo';

export const plugin = {
async getSources(parameters) {
if (parameters.query.length === 0) return [];
if (!checkIsSearchUser(parameters) || checkIsUnderFilter(parameters)) return [];
const { textFields, fieldsAsText } = getFieldsAsText();
const { titleFields } = getFieldsAsTitle();
return await debounced([
{
sourceId: 'text',
async getItems({ query }) {
if (query === '') return [];
const filter = `[all[tiddlers]!is[system]] ${titleTextExclusionFilter()} :filter[has[text]get[text]!compare:string:eq[]]+[search:${getFieldsAsText()}[${query}]]`;
const filter = `[all[tiddlers]!is[system]] ${titleTextExclusionFilter()} +[search:${fieldsAsText}[${query}]]`;
return await filterTiddlersAsync(filter, { system: false, exclude: [] });
},
getItemUrl({ item }) {
Expand All @@ -27,21 +29,23 @@ export const plugin = {
return lingo('UserText');
},
item({ item, createElement, state }) {
const title = typeof item.caption === 'string' && item.caption !== '' ? `${item.caption} (${item.title})` : item.title;
const titles = titleFields.map(field => item[field]).filter((item): item is string => typeof item === 'string' && item !== '').map((item, index) =>
index === 0 ? item : `(${item})`
).join(' ');
const textCountAroundHit = 30;
let contextNearText = '';
const keywords = state.query.split(' ').filter(Boolean);

const itemText = textFields.map(field => item[field]).filter(Boolean).join(' ');
keywords.forEach(keyword => {
const index = item.text.indexOf(keyword);
const index = itemText.indexOf(keyword);
if (index !== -1) {
const start = Math.max(0, index - textCountAroundHit);
const end = Math.min(item.text.length, index + keyword.length + textCountAroundHit);
const end = Math.min(itemText.length, index + keyword.length + textCountAroundHit);
const prefix = start > 0 ? '...' : '';
const suffix = end < item.text.length ? '...' : '';
const beforeMatch = item.text.slice(start, index);
const matchedText = item.text.slice(index, index + keyword.length);
const afterMatch = item.text.slice(index + keyword.length, end);
const suffix = end < itemText.length ? '...' : '';
const beforeMatch = itemText.slice(start, index);
const matchedText = itemText.slice(index, index + keyword.length);
const afterMatch = itemText.slice(index + keyword.length, end);

contextNearText += `${prefix}${beforeMatch}<mark>${matchedText}</mark>${afterMatch}${suffix}`;
}
Expand All @@ -55,7 +59,7 @@ export const plugin = {
onclick,
onTap: onclick,
}, [
createElement('div', { style: 'margin-bottom:0.25em;' }, title),
createElement('div', { style: 'margin-bottom:0.25em;' }, titles),
createElement('div', {}, [
createElement('small', { innerHTML: contextNearText }),
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ export const plugin = {
return [];
}
if (parameters.query.length === 0) return [];
const { fieldsAsTitle, titleFields } = getFieldsAsTitle();
return await debounced([
{
sourceId: 'title-pinyin',
async getItems({ query }) {
if (query === '') return [];
const filterToOpen = `[all[tiddlers]!is[system]] ${titleTextExclusionFilter()} +[pinyinfuse:${getFieldsAsTitle()}[${query}]]`;
const filterToOpen = `[all[tiddlers]!is[system]] ${titleTextExclusionFilter()} +[pinyinfuse:${fieldsAsTitle}[${query}]]`;
parameters.setContext({ filterToOpen });
return await filterTiddlersAsync(filterToOpen, {});
},
Expand All @@ -39,15 +40,13 @@ export const plugin = {
const onclick = () => {
parameters.navigator.navigate({ item, itemUrl: item.title, state });
};
if (typeof item.caption === 'string' && item.caption !== '') {
return createElement('div', {
onclick,
}, `${item.caption} (${item.title})`);
}
const titles = titleFields.map(field => item[field]).filter((item): item is string => typeof item === 'string' && item !== '').map((item, index) =>
index === 0 ? item : `(${item})`
).join(' ');
return createElement('div', {
onclick,
onTap: onclick,
}, item.title);
}, titles);
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ export const plugin = {
async getSources(parameters) {
if (parameters.query.length === 0) return [];
if (!checkIsSearchUser(parameters) || checkIsUnderFilter(parameters)) return [];
const { fieldsAsTitle, titleFields } = getFieldsAsTitle();
return await debounced([
{
sourceId: 'title',
async getItems({ query }) {
if (query === '') return [];
const filterToOpen = `[all[tiddlers]!is[system]] ${titleTextExclusionFilter()} +[search:${getFieldsAsTitle()}[${query}]]`;
const filterToOpen = `[all[tiddlers]!is[system]] ${titleTextExclusionFilter()} +[search:${fieldsAsTitle}[${query}]]`;
parameters.setContext({ filterToOpen });
return await filterTiddlersAsync(filterToOpen, {});
},
Expand All @@ -31,15 +32,13 @@ export const plugin = {
const onclick = () => {
parameters.navigator.navigate({ item, itemUrl: item.title, state });
};
if (typeof item.caption === 'string' && item.caption !== '') {
return createElement('div', {
onclick,
}, `${item.caption} (${item.title})`);
}
const titles = titleFields.map(field => item[field]).filter((item): item is string => typeof item === 'string' && item !== '').map((item, index) =>
index === 0 ? item : `(${item})`
).join(' ');
return createElement('div', {
onclick,
onTap: onclick,
}, item.title);
}, titles);
},
},
},
Expand Down
14 changes: 8 additions & 6 deletions src/autocomplete/widgets/utils/getFieldsAsTitle.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/**
* @returns `title,caption` that can be use with `search:title,caption[]` operator.
*/
export function getFieldsAsTitle(): string {
export function getFieldsAsTitle() {
const TitleAliasConfig = $tw.wiki.getTiddlerText('$:/plugins/linonetwo/autocomplete/configs/TitleAlias', 'title caption');
const fieldsAsTitle = TitleAliasConfig.split(' ').filter(Boolean).join(',');
return fieldsAsTitle;
const titleFields = TitleAliasConfig.split(' ').filter(Boolean);
const fieldsAsTitle = titleFields.join(',');
return { fieldsAsTitle, titleFields };
}

export function getFieldsAsText(): string {
export function getFieldsAsText() {
const TextAliasConfig = $tw.wiki.getTiddlerText('$:/plugins/linonetwo/autocomplete/configs/TextAlias', 'text');
const fieldsAsText = TextAliasConfig.split(' ').filter(Boolean).join(',');
return fieldsAsText;
const textFields = TextAliasConfig.split(' ').filter(Boolean);
const fieldsAsText = textFields.join(',');
return { fieldsAsText, textFields };
}

0 comments on commit 676da1d

Please sign in to comment.