Skip to content
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

refactor(hooks): improve code readability and update default theme #258

Merged
merged 2 commits into from
Dec 2, 2024
Merged
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
47 changes: 31 additions & 16 deletions render-markdown-codehighlight/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,27 @@ const get = async (url: string) => {
const { data } = await response.json();
return data;
};

const useHighlightCode: FC<pluginHookProps> = (props: HTMLElement | null | {
current: HTMLElement | null;
}, request: Request = {
get
}) => {
const [selectTheme, setSelectTheme] = useState<string>('default');
const useHighlightCode: FC<pluginHookProps> = (
props:
| HTMLElement
| null
| {
current: HTMLElement | null;
},
request: Request = {
get,
},
) => {
const [selectTheme, setSelectTheme] = useState<string>('stackoverflow');

// Fetch theme from API
useEffect(() => {
request.get('/answer/api/v1/render/config')
request
.get('/answer/api/v1/render/config')
.then((result) => {
console.log('Fetched theme:', result.select_theme);
setSelectTheme(result.select_theme);
if (result.select_theme) {
setSelectTheme(result.select_theme);
}
})
.catch((error) => {
console.error('Error fetching theme:', error);
Expand All @@ -58,7 +65,9 @@ const useHighlightCode: FC<pluginHookProps> = (props: HTMLElement | null | {
}

const applyThemeCSS = async (theme: string) => {
const existingStyleElement = document.querySelector('style[data-theme-style="highlight"]');
const existingStyleElement = document.querySelector(
'style[data-theme-style="highlight"]',
);
if (existingStyleElement) existingStyleElement.remove();

const styleElement = document.createElement('style');
Expand All @@ -81,24 +90,30 @@ const useHighlightCode: FC<pluginHookProps> = (props: HTMLElement | null | {
};

// Get and apply the initial theme
const currentTheme = document.documentElement.getAttribute('data-bs-theme') || 'light';
const currentTheme =
document.documentElement.getAttribute('data-bs-theme') || 'light';
applyThemeCSS(currentTheme);

// Observe DOM changes (e.g., code block content changes)
const contentObserver = new MutationObserver(() => {
const newTheme = document.documentElement.getAttribute('data-bs-theme') || 'light';
console.log('Detected code content change, reapplying syntax highlighting, current theme:', newTheme);
const newTheme =
document.documentElement.getAttribute('data-bs-theme') || 'light';
console.log(
'Detected code content change, reapplying syntax highlighting, current theme:',
newTheme,
);
applyThemeCSS(newTheme);
});

contentObserver.observe(element, {
childList: true, // Observe changes to child elements
subtree: true, // Observe the entire subtree
subtree: true, // Observe the entire subtree
});

// Observe theme changes
const themeObserver = new MutationObserver(() => {
const newTheme = document.documentElement.getAttribute('data-bs-theme') || 'light';
const newTheme =
document.documentElement.getAttribute('data-bs-theme') || 'light';
console.log('Detected theme change:', newTheme);
applyThemeCSS(newTheme);
});
Expand Down
2 changes: 1 addition & 1 deletion render-markdown-codehighlight/info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@

slug_name: render_markdown_codehighlight
type: render
version: 0.1.2
version: 0.2.0
author: Chen Jiaji, Zhu Xuanlyu
link: https://github.com/apache/incubator-answer-plugins/tree/main/render-markdown-codehighlight