diff --git a/docs/hosting.md b/docs/hosting.md index 606ffcf2e..d33f2619f 100644 --- a/docs/hosting.md +++ b/docs/hosting.md @@ -243,3 +243,21 @@ server { } } ``` + +### Using Caddy + +Here's and example of how to do this with Caddy: + +```caddy title="Caddyfile" +example.com { + root * /path/to/quartz/public + try_files {path} {path}.html {path}/ =404 + file_server + encode gzip + + handle_errors { + rewrite * /{err.status_code}.html + file_server + } +} +``` diff --git a/quartz/components/scripts/search.inline.ts b/quartz/components/scripts/search.inline.ts index d0aa45fd2..c868208f8 100644 --- a/quartz/components/scripts/search.inline.ts +++ b/quartz/components/scripts/search.inline.ts @@ -22,6 +22,7 @@ let index = new FlexSearch.Document({ encode: encoder, document: { id: "id", + tag: "tags", index: [ { field: "title", @@ -443,11 +444,33 @@ document.addEventListener("nav", async (e: CustomEventMap["nav"]) => { let searchResults: FlexSearch.SimpleDocumentSearchResultSetUnit[] if (searchType === "tags") { - searchResults = await index.searchAsync({ - query: currentSearchTerm.substring(1), - limit: numSearchResults, - index: ["tags"], - }) + currentSearchTerm = currentSearchTerm.substring(1).trim() + const separatorIndex = currentSearchTerm.indexOf(" ") + if (separatorIndex != -1) { + // search by title and content index and then filter by tag (implemented in flexsearch) + const tag = currentSearchTerm.substring(0, separatorIndex) + const query = currentSearchTerm.substring(separatorIndex + 1).trim() + searchResults = await index.searchAsync({ + query: query, + // return at least 10000 documents, so it is enough to filter them by tag (implemented in flexsearch) + limit: Math.max(numSearchResults, 10000), + index: ["title", "content"], + tag: tag, + }) + for (let searchResult of searchResults) { + searchResult.result = searchResult.result.slice(0, numSearchResults) + } + // set search type to basic and remove tag from term for proper highlightning and scroll + searchType = "basic" + currentSearchTerm = query + } else { + // default search by tags index + searchResults = await index.searchAsync({ + query: currentSearchTerm, + limit: numSearchResults, + index: ["tags"], + }) + } } else if (searchType === "basic") { searchResults = await index.searchAsync({ query: currentSearchTerm, diff --git a/quartz/plugins/transformers/ofm.ts b/quartz/plugins/transformers/ofm.ts index 01462860a..27c3e38b6 100644 --- a/quartz/plugins/transformers/ofm.ts +++ b/quartz/plugins/transformers/ofm.ts @@ -110,15 +110,27 @@ export const externalLinkRegex = /^https?:\/\//i export const arrowRegex = new RegExp(/(-{1,2}>|={1,2}>|<-{1,2}|<={1,2})/, "g") -// !? -> optional embedding -// \[\[ -> open brace -// ([^\[\]\|\#]+) -> one or more non-special characters ([,],|, or #) (name) -// (#[^\[\]\|\#]+)? -> # then one or more non-special characters (heading link) -// (\|[^\[\]\#]+)? -> \| then one or more non-special characters (alias) +// !? -> optional embedding +// \[\[ -> open brace +// ([^\[\]\|\#]+) -> one or more non-special characters ([,],|, or #) (name) +// (#[^\[\]\|\#]+)? -> # then one or more non-special characters (heading link) +// (\\?\|[^\[\]\#]+)? -> optional escape \ then | then one or more non-special characters (alias) export const wikilinkRegex = new RegExp( /!?\[\[([^\[\]\|\#\\]+)?(#+[^\[\]\|\#\\]+)?(\\?\|[^\[\]\#]+)?\]\]/, "g", ) + +// ^\|([^\n])+\|\n(\|) -> matches the header row +// ( ?:?-{3,}:? ?\|)+ -> matches the header row separator +// (\|([^\n])+\|\n)+ -> matches the body rows +export const tableRegex = new RegExp( + /^\|([^\n])+\|\n(\|)( ?:?-{3,}:? ?\|)+\n(\|([^\n])+\|\n?)+/, + "gm", +) + +// matches any wikilink, only used for escaping wikilinks inside tables +export const tableWikilinkRegex = new RegExp(/(!?\[\[[^\]]*?\]\])/, "g") + const highlightRegex = new RegExp(/==([^=]+)==/, "g") const commentRegex = new RegExp(/%%[\s\S]*?%%/, "g") // from https://github.com/escwxyz/remark-obsidian-callout/blob/main/src/index.ts @@ -183,6 +195,20 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin< src = src.toString() } + // replace all wikilinks inside a table first + src = src.replace(tableRegex, (value) => { + // escape all aliases and headers in wikilinks inside a table + return value.replace(tableWikilinkRegex, (value, ...capture) => { + const [raw]: (string | undefined)[] = capture + let escaped = raw ?? "" + escaped = escaped.replace("#", "\\#") + escaped = escaped.replace("|", "\\|") + + return escaped + }) + }) + + // replace all other wikilinks src = src.replace(wikilinkRegex, (value, ...capture) => { const [rawFp, rawHeader, rawAlias]: (string | undefined)[] = capture @@ -199,20 +225,14 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin< return `${embedDisplay}[${displayAlias.replace(/^\|/, "")}](${rawFp})` } - //transform `[[note#^block_ref|^block_ref]]` to `[[note#^block_ref\|^block_ref]]`, display correctly in table. - if (displayAlias && displayAlias.startsWith("|")) { - displayAlias = `\\${displayAlias}` - } - return `${embedDisplay}[[${fp}${displayAnchor}${displayAlias}]]` }) } return src }, - markdownPlugins(ctx) { + markdownPlugins(_ctx) { const plugins: PluggableList = [] - const cfg = ctx.cfg.configuration // regex replacements plugins.push(() => {