Skip to content

Fix: Handle Craft Links with Custom Domains by Implementing getCraftSlug Function #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion components/NotePost.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import BLOG from '@/blog.config.js'
import Link from 'next/link'
import ImageFallback from './Common/ImageFallback.js'
import getCraftSlug from '@/lib/getCraftSlug'

const NotePost = ({ note }) => {
const craftSlug = note.url.slice(23)
const craftSlug = getCraftSlug(note.url)
return (
<Link
passHref
Expand All @@ -26,3 +27,4 @@ const NotePost = ({ note }) => {
}

export default NotePost

4 changes: 3 additions & 1 deletion lib/getBlocksMaps.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import BLOG from '@/blog.config'
import getCraftSlug from '@/lib/getCraftSlug'

// 从 Config 页面的 API 获取两个表格的内容, 并处理成两个 json 返回给 htmlrewrite.js
export async function getBlocksMaps() {
const craftConfigSecret = BLOG.craftConfigShareUrl.slice(23)
const craftConfigSecret = getCraftSlug(BLOG.craftConfigShareUrl)
const craftConfigApiUrl = 'https://www.craft.do/api/share/' + craftConfigSecret
const init = {
headers: {
Expand Down Expand Up @@ -150,3 +151,4 @@ function getSiteConfigObj(mapJson, configColumId, valueColumId) {
})
return siteConfigObj
}

24 changes: 24 additions & 0 deletions lib/getCraftSlug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Extracts the ID from a given Craft link.
* @param {string} link - The Craft link.
* @returns {string} - The extracted ID.
* @throws Will throw an error if the link format is invalid.
*/
const getCraftSlug = (link) => {
const patterns = [
/^https:\/\/www\.craft\.me\/s\/(.+)$/,
/^https:\/\/www\.craft\.do\/s\/(.+)$/,
/^https:\/\/.+\.craft\.me\/(.+)$/
];

for (const pattern of patterns) {
const match = link.match(pattern);
if (match) {
return match[1];
}
}

throw new Error("Invalid link format");
};

export default getCraftSlug;