12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- export const SCENE_URL = window.location.pathname.replace(
- /\/(pc|mobile)\/index.html$/,
- "/scene/syjy.html?m=SG-SkJEj1Vhf7l"
- );
- export const extractText = (html) => {
- // 提取纯文本,去掉HTML标签
- const tmpDiv = document.createElement("div");
- tmpDiv.innerHTML = html;
- return tmpDiv.innerText.trim();
- };
- export const findKeywordContext = (text, keyword, radius = 20) => {
- const index = text.indexOf(keyword);
- if (index === -1) {
- return null;
- }
- const start = Math.max(0, index - radius);
- const end = Math.min(text.length, index + keyword.length + radius);
- return text.substring(start, end);
- };
- export const flattenData = (query, list, parentTitles = [], path = []) => {
- const result = [];
- list.forEach((item, index) => {
- const titles = [...parentTitles, item.title];
- const currentPath = [...path, index];
- if (item.content) {
- const contentText = extractText(item.content).replace(/[\n\s]/g, "");
- if (contentText.indexOf(query) > -1) {
- result.push({
- value: currentPath.join(","),
- label: `${titles.join(" | ")} | ${findKeywordContext(
- contentText,
- query
- )}`,
- });
- }
- }
- if (item.children) {
- result.push(...flattenData(query, item.children, titles, currentPath));
- }
- });
- return result;
- };
|