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

Extend HTML mode - persist checkbox value and use across views #135

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
9 changes: 7 additions & 2 deletions src/components/Code.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Render Markdown as a codeblock. Apply highlighting on content changes.
<template>
<div>
<Checkbox label="Show HTML" v-model="asHtml" />
<Checkbox label="Show HTML" v-model="asHtml" @change="storeAsHtml()" />
<br />

<pre><code ref="codeBlock" class="markdown">{{ outputCode }}</code></pre>
Expand All @@ -14,6 +14,7 @@ import { defineComponent } from "vue";

import { cleanHtml, mdToHTML } from "@/core/markdown";
import Checkbox from "./Checkbox.vue";
import store from "@/store";

export default defineComponent({
name: "Code",
Expand All @@ -25,7 +26,7 @@ export default defineComponent({
},
data() {
return {
asHtml: false,
asHtml: store.state.asHtml,
};
},
computed: {
Expand All @@ -35,10 +36,14 @@ export default defineComponent({

return cleanHtml(htmlCode);
}

return this.code;
},
},
methods: {
storeAsHtml() {
store.setAsHtml(this.asHtml);
},
highlight() {
const block = this.$refs.codeBlock as HTMLElement;
hljs.highlightElement(block);
Expand Down
4 changes: 2 additions & 2 deletions src/core/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ export function mdImageWithLink({
/**
* Markdown to HTML.
*
* Render Markdown code as HTML code.
* Render Markdown code as HTML code, without trailing newline.
*/
export function mdToHTML(value: string): string {
return md.render(value);
return md.render(value).trim();
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const store = {
state: reactive({
repoUsername: DEFAULT_REPO_INPUTS.username,
repoName: DEFAULT_REPO_INPUTS.repoName,
asHtml: false,
}),

/**
Expand All @@ -37,6 +38,17 @@ const store = {

this.state.repoName = value;
},

/**
* Store value for whether output is displayed as HTML or Markdown.
*/
setAsHtml(value: boolean) {
if (DEBUG) {
console.debug(`Storing asHtml as: ${value}`);
}

this.state.asHtml = value;
},
};

export default store;
2 changes: 2 additions & 0 deletions tests/unit/components/Code.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Code from "@/components/Code.vue";
import { shallowMount } from "@vue/test-utils";
import store from "@/store";
MichaelCurrin marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bit needs to be actually used.

Also please run tests and fix them. The Code component needs to switch between HTML and Markdown mode in the tests.

This file is failing.

This old code is like this:

      data() {
        return {
          asHtml: true,
        };
      },

I tried adding this and it didn't work.


    wrapper.setData({ asHtml: true })
    store.setAsHtml(true)


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change


describe("Code.vue", () => {
it("renders a Markdown codeblock as Markdown code, with highlighting", () => {
Expand Down